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.

895 lines
27 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: InptChkr.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * Copyright (c) 1997 Microsoft Corporation.
  7. * All rights reserved.
  8. *
  9. * 11311 Chinden Blvd.
  10. * Boise, Idaho 83714
  11. *
  12. *****************************************************************************/
  13. #include "precomp.h"
  14. #include "TCPMonUI.h"
  15. #include "UIMgr.h"
  16. #include "InptChkr.h"
  17. #include "Resource.h"
  18. #include "IPAddr.h"
  19. #include "HostName.h"
  20. //
  21. // FUNCTION: CInputChecker constructor
  22. //
  23. // PURPOSE: initialize a CInputChecker class
  24. //
  25. CInputChecker::CInputChecker()
  26. {
  27. m_bLinked = FALSE;
  28. m_InputStorageStringAddress[0] = '\0';
  29. m_InputStorageStringPortNumber[0] = '\0';
  30. m_InputStorageStringDeviceIndex[0] = '\0';
  31. m_InputStorageStringQueueName[0] = '\0';
  32. } // constructor
  33. //
  34. // FUNCTION: CInputChecker destructor
  35. //
  36. // PURPOSE: deinitialize a CInputChecker class
  37. //
  38. CInputChecker::~CInputChecker()
  39. {
  40. } // destructor
  41. //
  42. // FUNCTION: OnUpdatePortName(idEditCtrl, hwndEditCtrl)
  43. //
  44. // PURPOSE: to handle EN_UPDATE message when the edit control is the Port Name input.
  45. //
  46. void CInputChecker::OnUpdatePortName(int idEditCtrl, HWND hwndEditCtrl)
  47. {
  48. // the edit control for Port Name had text changed in it.
  49. BOOL bModified = static_cast<BOOL> (SendMessage(hwndEditCtrl, EM_GETMODIFY, 0,0));
  50. if(bModified)
  51. {
  52. TCHAR tcsAddr[MAX_ADDRESS_LENGTH] = NULLSTR;
  53. TCHAR tcsLastValidAddr[MAX_ADDRESS_LENGTH] = NULLSTR;
  54. GetWindowText(hwndEditCtrl, tcsAddr, MAX_ADDRESS_LENGTH);
  55. if(! IsValidPortNameInput(tcsAddr,
  56. tcsLastValidAddr,
  57. SIZEOF_IN_CHAR(tcsLastValidAddr)))
  58. {
  59. // the port name that was entered is not valid so beep and set the text
  60. // back to the last valid entry. This test for validity does not
  61. // include testing for the right length just proper character set.
  62. MessageBeep((UINT)-1);
  63. DWORD dwSel = Edit_GetSel(hwndEditCtrl);
  64. SetWindowText(hwndEditCtrl, tcsLastValidAddr);
  65. Edit_SetSel(hwndEditCtrl, LOWORD(dwSel) - 1, HIWORD(dwSel) - 1);
  66. }
  67. m_bLinked = FALSE;
  68. }
  69. } // OnUpdatePortName
  70. //
  71. // FUNCTION: OnUpdatePortNumber(idEditCtrl, hwndEditCtrl)
  72. //
  73. // PURPOSE: to handle EN_UPDATE message when the edit control is the Port Number input.
  74. //
  75. void CInputChecker::OnUpdatePortNumber(int idEditCtrl, HWND hwndEditCtrl)
  76. {
  77. // the edit control for Port Number had text changed in it.
  78. TCHAR tcsPortNum[MAX_PORTNUM_STRING_LENGTH] = NULLSTR;
  79. TCHAR tcsLastValidPortNum[MAX_PORTNUM_STRING_LENGTH] = NULLSTR;
  80. GetWindowText(hwndEditCtrl, tcsPortNum, MAX_PORTNUM_STRING_LENGTH);
  81. if(! IsValidPortNumberInput(tcsPortNum,
  82. tcsLastValidPortNum,
  83. SIZEOF_IN_CHAR(tcsLastValidPortNum)))
  84. {
  85. // the port number that was entered is not valid so beep and set the text
  86. // back to the last valid entry. This test for validity does not
  87. // include testing for the right length just proper character set.
  88. MessageBeep((UINT)-1);
  89. DWORD dwSel = Edit_GetSel(hwndEditCtrl);
  90. SetWindowText(hwndEditCtrl, tcsLastValidPortNum);
  91. Edit_SetSel(hwndEditCtrl, LOWORD(dwSel) - 1, HIWORD(dwSel) - 1);
  92. }
  93. } // OnUpdatePortNumber
  94. //
  95. // FUNCTION: OnUpdateDeviceIndex(idEditCtrl, hwndEditCtrl)
  96. //
  97. // PURPOSE: to handle EN_UPDATE message when the edit control is the Device Index input.
  98. //
  99. void CInputChecker::OnUpdateDeviceIndex(int idEditCtrl, HWND hwndEditCtrl)
  100. {
  101. // the edit control for Port Number had text changed in it.
  102. TCHAR tcsDeviceIndex[MAX_SNMP_DEVICENUM_STRING_LENGTH] = NULLSTR;
  103. TCHAR tcsLastValidDeviceIndex[MAX_SNMP_DEVICENUM_STRING_LENGTH] = NULLSTR;
  104. GetWindowText(hwndEditCtrl, tcsDeviceIndex, MAX_SNMP_DEVICENUM_STRING_LENGTH);
  105. if(! IsValidDeviceIndexInput(tcsDeviceIndex,
  106. tcsLastValidDeviceIndex,
  107. SIZEOF_IN_CHAR(tcsLastValidDeviceIndex)))
  108. {
  109. // the device index that was entered is not valid so beep and set the
  110. // text back to the last valid entry. This test for validity does not
  111. // include testing for the right length just proper character set.
  112. MessageBeep((UINT)-1);
  113. DWORD dwSel = Edit_GetSel(hwndEditCtrl);
  114. SetWindowText(hwndEditCtrl, tcsLastValidDeviceIndex);
  115. Edit_SetSel(hwndEditCtrl, LOWORD(dwSel) - 1, HIWORD(dwSel) - 1);
  116. }
  117. } // OnUpdateDeviceIndex
  118. //
  119. // FUNCTION: OnUpdateAddress(idEditCtrl, hwndEditCtrl)
  120. //
  121. // PURPOSE: to handle EN_UPDATE message when the edit control is the Address input.
  122. //
  123. void CInputChecker::OnUpdateAddress(HWND hDlg, int idEditCtrl, HWND hwndEditCtrl, LPTSTR psztServerName)
  124. {
  125. // the edit control for IP Address or Device Name had text changed in it.
  126. TCHAR tcsAddr[MAX_ADDRESS_LENGTH] = NULLSTR;
  127. TCHAR tcsLastValidAddr[MAX_ADDRESS_LENGTH] = NULLSTR;
  128. GetWindowText(hwndEditCtrl, tcsAddr, MAX_ADDRESS_LENGTH);
  129. BOOL bValid = IsValidAddressInput(tcsAddr,
  130. tcsLastValidAddr,
  131. SIZEOF_IN_CHAR(tcsLastValidAddr));
  132. if(! bValid)
  133. {
  134. // the address that was entered is not valid so beep and set the text
  135. // back to the last valid entry. This test for validity does not
  136. // include testing for the right length just proper character set.
  137. MessageBeep((UINT)-1);
  138. DWORD dwSel = Edit_GetSel(hwndEditCtrl);
  139. SetWindowText(hwndEditCtrl, tcsLastValidAddr);
  140. Edit_SetSel(hwndEditCtrl, LOWORD(dwSel) - 1, HIWORD(dwSel) - 1);
  141. }
  142. else // The address is valid.
  143. {
  144. if(m_bLinked)
  145. {
  146. MakePortName(tcsAddr, COUNTOF (tcsAddr));
  147. SetWindowText(GetDlgItem(hDlg, IDC_EDIT_PORT_NAME), tcsAddr);
  148. }
  149. }
  150. } // OnUpdateAddress
  151. //
  152. // FUNCTION: OnUpdateQueueName(idEditCtrl, hwndEditCtrl)
  153. //
  154. // PURPOSE: to handle EN_UPDATE message when the edit control is the QueueName input.
  155. //
  156. void CInputChecker::OnUpdateQueueName(int idEditCtrl, HWND hwndEditCtrl)
  157. {
  158. // the edit control for QueueName had text changed in it.
  159. TCHAR tcsQueueName[MAX_QUEUENAME_LEN] = NULLSTR;
  160. TCHAR tcsLastValidQueueName[MAX_QUEUENAME_LEN] = NULLSTR;
  161. GetWindowText(hwndEditCtrl, tcsQueueName, MAX_QUEUENAME_LEN);
  162. if(! IsValidQueueNameInput(tcsQueueName,
  163. tcsLastValidQueueName,
  164. SIZEOF_IN_CHAR(tcsLastValidQueueName)))
  165. {
  166. // the device index that was entered is not valid so beep and set the
  167. // text back to the last valid entry. This test for validity does not
  168. // include testing for the right length just proper character set.
  169. MessageBeep((UINT)-1);
  170. DWORD dwSel = Edit_GetSel(hwndEditCtrl);
  171. SetWindowText(hwndEditCtrl, tcsLastValidQueueName);
  172. Edit_SetSel(hwndEditCtrl, LOWORD(dwSel) - 1, HIWORD(dwSel) - 1);
  173. }
  174. } // OnUpdateQueueName
  175. //
  176. // FUNCTION: IsValidPortNameInput(TCHAR ptcsAddressInput[MAX_ADDRESS_LENGTH], TCHAR *ptcsReturnLastValid)
  177. //
  178. // PURPOSE: IsValidPortNameInput is used for validation while the user is typing.
  179. //
  180. // Arguments: ptcsAddressInput is the user input.
  181. // ptcsReturnLastValid is the last valid user input
  182. // from the last time this function was called.
  183. // CRtnValSize size in chars ( or wide chars) of the destination
  184. // buffer.
  185. //
  186. // Return Value: Returns TRUE if the input is valid. FALSE otherwise, with ptcsReturnLastValid set.
  187. //
  188. // Comments: Any input is valid until I hear otherwise.
  189. //
  190. BOOL CInputChecker::IsValidPortNameInput(TCHAR *ptcsAddressInput,
  191. TCHAR *ptcsReturnLastValid,
  192. DWORD CRtnValSize)
  193. {
  194. DWORD dwLen = 0;
  195. BOOL bValid = FALSE;
  196. //
  197. // Valid port name is non-blank and does not include ,
  198. //
  199. if ( ptcsAddressInput ) {
  200. while ( ptcsAddressInput[dwLen] != TEXT('\0') &&
  201. ptcsAddressInput[dwLen] != TEXT(',') )
  202. ++dwLen;
  203. bValid = dwLen && ptcsAddressInput[dwLen] != TEXT(',');
  204. }
  205. if ( CRtnValSize ) {
  206. if ( dwLen + 1 > CRtnValSize )
  207. dwLen = CRtnValSize - 1;
  208. lstrcpyn(ptcsReturnLastValid, ptcsAddressInput ? ptcsAddressInput : L"", dwLen+1);
  209. }
  210. return bValid;
  211. } // IsValidPortNameInput
  212. //
  213. // FUNCTION: IsValidCommunityNameInput(TCHAR ptcsCommunityNameInput[MAX_SNMP_COMMUNITY_STR_LEN], TCHAR *ptcsReturnLastValid)
  214. //
  215. // PURPOSE: IsValidCommunityNameInput is used for validation while the user is typing.
  216. //
  217. // Arguments: ptcsCommunityNameInput is the user input.
  218. // ptcsReturnLastValid is the last valid user input
  219. // from the last time this function was called.
  220. // CRtnValSize size in chars ( or wide chars) of the destination
  221. // buffer.
  222. //
  223. // Return Value: Returns TRUE if the input is valid. FALSE otherwise, with ptcsReturnLastValid set.
  224. //
  225. // Comments: Any input is valid until I hear otherwise.
  226. //
  227. BOOL CInputChecker::IsValidCommunityNameInput(TCHAR *ptcsCommunityNameInput,
  228. TCHAR *ptcsReturnLastValid,
  229. DWORD CRtnValSize)
  230. {
  231. return TRUE;
  232. } // IsValidCommunityNameInput
  233. //
  234. // FUNCTION: IsValidPortNumberInput(TCHAR ptcsAddressInput[MAX_ADDRESS_LENGTH], TCHAR *ptcsReturnLastValid)
  235. //
  236. // PURPOSE: IsValidPortNumberInput is used for validation while the user is typing.
  237. //
  238. // Arguments: ptcsAddressInput is the user input.
  239. // ptcsReturnLastValid is the last valid user input
  240. // from the last time this function was called.
  241. // CRtnValSize size in chars ( or wide chars) of the destination
  242. // buffer.
  243. //
  244. // Return Value: Returns TRUE if the input is valid. FALSE otherwise, with ptcsReturnLastValid set.
  245. //
  246. // Comments: The input is valid if it contains only digit characters.
  247. //
  248. BOOL CInputChecker::IsValidPortNumberInput(TCHAR *ptcsPortNumInput,
  249. TCHAR *ptcsReturnLastValid,
  250. DWORD CRtnValSize)
  251. {
  252. BOOL bIsValid = TRUE;
  253. TCHAR *charPtr = NULL;
  254. TCHAR ptcsString[MAX_PORTNUM_STRING_LENGTH] = NULLSTR;
  255. lstrcpyn(ptcsString, ptcsPortNumInput, MAX_PORTNUM_STRING_LENGTH );
  256. bIsValid = (_tcslen(ptcsString) <= MAX_PORTNUM_STRING_LENGTH);
  257. for (charPtr = ptcsString; bIsValid && *charPtr; charPtr++)
  258. {
  259. switch (*charPtr)
  260. {
  261. case (TCHAR)'0':
  262. case (TCHAR)'1':
  263. case (TCHAR)'2':
  264. case (TCHAR)'3':
  265. case (TCHAR)'4':
  266. case (TCHAR)'5':
  267. case (TCHAR)'6':
  268. case (TCHAR)'7':
  269. case (TCHAR)'8':
  270. case (TCHAR)'9':
  271. bIsValid = TRUE;
  272. break;
  273. default:
  274. bIsValid = FALSE;
  275. break;
  276. }
  277. }
  278. if (!bIsValid)
  279. {
  280. if(ptcsReturnLastValid != NULL)
  281. {
  282. lstrcpyn(ptcsReturnLastValid,
  283. m_InputStorageStringPortNumber,
  284. CRtnValSize);
  285. }
  286. }
  287. else
  288. {
  289. lstrcpyn(m_InputStorageStringPortNumber,
  290. ptcsString,
  291. MAX_ADDRESS_LENGTH);
  292. }
  293. return(bIsValid);
  294. } // IsValidPortNumberInput
  295. //
  296. // FUNCTION: IsValidDeviceIndexInput(TCHAR ptcsAddressInput[MAX_ADDRESS_LENGTH], TCHAR *ptcsReturnLastValid)
  297. //
  298. // PURPOSE: IsValidDeviceIndexInput is used for validation while the user is typing.
  299. //
  300. // Arguments: ptcsAddressInput is the user input.
  301. // ptcsReturnLastValid is the last valid user input
  302. // from the last time this function was called.
  303. // CRtnValSize size in chars ( or wide chars) of the destination
  304. // buffer.
  305. //
  306. // Return Value: Returns TRUE if the input is valid. FALSE otherwise, with ptcsReturnLastValid set.
  307. //
  308. // Comments: The input is valid if it contains only digit characters.
  309. //
  310. BOOL CInputChecker::IsValidDeviceIndexInput(TCHAR *ptcsDeviceIndexInput,
  311. TCHAR *ptcsReturnLastValid,
  312. DWORD CRtnValSize)
  313. {
  314. BOOL bIsValid = TRUE;
  315. TCHAR *charPtr = NULL;
  316. TCHAR ptcsString[MAX_SNMP_DEVICENUM_STRING_LENGTH] = NULLSTR;
  317. lstrcpyn(ptcsString,
  318. ptcsDeviceIndexInput,
  319. MAX_SNMP_DEVICENUM_STRING_LENGTH);
  320. bIsValid = (_tcslen(ptcsString) <= MAX_SNMP_DEVICENUM_STRING_LENGTH);
  321. for (charPtr = ptcsString; bIsValid && *charPtr; charPtr++)
  322. {
  323. switch (*charPtr)
  324. {
  325. case (TCHAR)'0':
  326. case (TCHAR)'1':
  327. case (TCHAR)'2':
  328. case (TCHAR)'3':
  329. case (TCHAR)'4':
  330. case (TCHAR)'5':
  331. case (TCHAR)'6':
  332. case (TCHAR)'7':
  333. case (TCHAR)'8':
  334. case (TCHAR)'9':
  335. bIsValid = TRUE;
  336. break;
  337. default:
  338. bIsValid = FALSE;
  339. break;
  340. }
  341. }
  342. if (!bIsValid)
  343. {
  344. if(ptcsReturnLastValid != NULL)
  345. {
  346. lstrcpyn(ptcsReturnLastValid,
  347. m_InputStorageStringDeviceIndex,
  348. CRtnValSize);
  349. }
  350. }
  351. else
  352. {
  353. lstrcpyn(m_InputStorageStringDeviceIndex,
  354. ptcsString,
  355. MAX_SNMP_DEVICENUM_STRING_LENGTH);
  356. }
  357. return(bIsValid);
  358. } // IsValidDeviceIndexInput
  359. //
  360. // FUNCTION: IsValidAddressInput(TCHAR ptcsAddressInput[MAX_ADDRESS_LENGTH], TCHAR *ptcsReturnLastValid)
  361. //
  362. // PURPOSE: IsValidAddressInput is used for validation while the user is typing.
  363. //
  364. // Arguments: ptcsAddressInput is the user input.
  365. // ptcsReturnLastValid is the last valid user input
  366. // from the last time this function was called.
  367. // CRtnValSize size in chars ( or wide chars) of the destination
  368. // buffer.
  369. //
  370. // Return Value: Returns TRUE if the input is valid. FALSE otherwise.
  371. //
  372. // Comments: The input is valid if it contains characters that are either valid for
  373. // an IP Address or a Host Name or both.
  374. //
  375. BOOL CInputChecker::IsValidAddressInput(TCHAR *ptcsAddressInput,
  376. TCHAR *ptcsReturnLastValid,
  377. DWORD CRtnValSize)
  378. {
  379. TCHAR *charPtr = NULL;
  380. TCHAR ptcsString[MAX_ADDRESS_LENGTH] = NULLSTR;
  381. BOOL bIsValid = FALSE;
  382. lstrcpyn(ptcsString, ptcsAddressInput, MAX_ADDRESS_LENGTH);
  383. bIsValid = (_tcslen(ptcsString) <= MAX_ADDRESS_LENGTH);
  384. for (charPtr = ptcsString; bIsValid && *charPtr; charPtr++)
  385. {
  386. switch (*charPtr)
  387. {
  388. case (TCHAR)' ':
  389. case (TCHAR)'"':
  390. case (TCHAR)'&':
  391. case (TCHAR)'*':
  392. case (TCHAR)'(':
  393. case (TCHAR)')':
  394. case (TCHAR)'+':
  395. case (TCHAR)',':
  396. case (TCHAR)'/':
  397. case (TCHAR)':':
  398. case (TCHAR)';':
  399. case (TCHAR)'<':
  400. case (TCHAR)'=':
  401. case (TCHAR)'>':
  402. case (TCHAR)'?':
  403. case (TCHAR)'[':
  404. case (TCHAR)'\\':
  405. case (TCHAR)']':
  406. case (TCHAR)'|':
  407. case (TCHAR)'~':
  408. case (TCHAR)'@':
  409. case (TCHAR)'#':
  410. case (TCHAR)'$':
  411. case (TCHAR)'%':
  412. case (TCHAR)'^':
  413. case (TCHAR)'!':
  414. // other invalid character cases here
  415. bIsValid = FALSE;
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. if (!bIsValid)
  422. {
  423. if(ptcsReturnLastValid != NULL)
  424. {
  425. lstrcpyn(ptcsReturnLastValid,
  426. m_InputStorageStringAddress,
  427. CRtnValSize);
  428. }
  429. }
  430. else
  431. {
  432. lstrcpyn(m_InputStorageStringAddress,
  433. ptcsString,
  434. MAX_ADDRESS_LENGTH);
  435. }
  436. return(bIsValid);
  437. } // IsValidAddressInput
  438. //
  439. // FUNCTION: IsValidQueueNameInput(TCHAR ptcsAddressInput[MAX_QUEUENAME_LEN], TCHAR *ptcsReturnLastValid)
  440. //
  441. // PURPOSE: IsValidQueueNameInput is used for validation while the user is typing.
  442. //
  443. // Arguments: ptcsQueueNameInput is the user input.
  444. // ptcsReturnLastValid is the last valid user input
  445. // from the last time this function was called.
  446. // CRtnValSize size in chars ( or wide chars) of the destination
  447. // buffer.
  448. //
  449. // Return Value: Returns TRUE if the input is valid. FALSE otherwise.
  450. //
  451. // Comments: The name is limited to 14 characters and must consist entirely of the
  452. // characters A-Z, a-z, 0-9, and _ (underscore).
  453. //
  454. BOOL CInputChecker::IsValidQueueNameInput(TCHAR *ptcsQueueNameInput,
  455. TCHAR *ptcsReturnLastValid,
  456. DWORD CRtnValSize)
  457. {
  458. BOOL bIsValid = TRUE;
  459. TCHAR *charPtr = NULL;
  460. TCHAR ptcsString[MAX_QUEUENAME_LEN] = NULLSTR;
  461. lstrcpyn(ptcsString, ptcsQueueNameInput, MAX_QUEUENAME_LEN );
  462. bIsValid = (_tcslen(ptcsString) <= MAX_QUEUENAME_LEN);
  463. for (charPtr = ptcsString; bIsValid && *charPtr; charPtr++)
  464. {
  465. switch (*charPtr)
  466. {
  467. case (TCHAR)' ':
  468. case (TCHAR)'"':
  469. case (TCHAR)'&':
  470. case (TCHAR)'*':
  471. case (TCHAR)'(':
  472. case (TCHAR)')':
  473. case (TCHAR)'+':
  474. case (TCHAR)',':
  475. case (TCHAR)'/':
  476. case (TCHAR)':':
  477. case (TCHAR)';':
  478. case (TCHAR)'<':
  479. case (TCHAR)'=':
  480. case (TCHAR)'>':
  481. case (TCHAR)'?':
  482. case (TCHAR)'[':
  483. case (TCHAR)'\\':
  484. case (TCHAR)']':
  485. case (TCHAR)'|':
  486. case (TCHAR)'~':
  487. case (TCHAR)'@':
  488. case (TCHAR)'#':
  489. case (TCHAR)'$':
  490. case (TCHAR)'%':
  491. case (TCHAR)'^':
  492. case (TCHAR)'!':
  493. bIsValid = FALSE;
  494. break;
  495. default:
  496. bIsValid = TRUE;
  497. break;
  498. }
  499. }
  500. if (!bIsValid)
  501. {
  502. if(ptcsReturnLastValid != NULL)
  503. {
  504. lstrcpyn(ptcsReturnLastValid,
  505. m_InputStorageStringQueueName,
  506. CRtnValSize);
  507. }
  508. }
  509. else
  510. {
  511. lstrcpyn(m_InputStorageStringQueueName,
  512. ptcsString,
  513. MAX_QUEUENAME_LEN);
  514. }
  515. return(bIsValid);
  516. } // IsValidQueueNameInput
  517. //
  518. // FUNCTION: MakePortName(TCHAR *strAddr)
  519. //
  520. // PURPOSE: To return a string that will be a unique port
  521. // name when the port is added.
  522. //
  523. void CInputChecker::MakePortName(TCHAR *strAddr, size_t cchAddr)
  524. {
  525. _ASSERTE(m_bLinked == TRUE);
  526. if(GetAddressType(strAddr) == IPAddress)
  527. {
  528. // The address is an IP address
  529. TCHAR NameString[10] = NULLSTR;
  530. if (LoadString(g_hInstance, IDS_STRING_NAME_IP, NameString, COUNTOF (NameString)))
  531. {
  532. TCHAR szTemp[MAX_ADDRESS_LENGTH+1] = NULLSTR;
  533. HRESULT hr =
  534. StringCchPrintf (szTemp, COUNTOF (szTemp),
  535. TEXT( "%s%.*s" ),
  536. NameString,
  537. MAX_ADDRESS_LENGTH - _tcslen(NameString),
  538. strAddr);
  539. if (SUCCEEDED (hr))
  540. {
  541. StringCchCopy (
  542. strAddr,
  543. cchAddr,
  544. szTemp
  545. );
  546. }
  547. }
  548. }
  549. } // MakePortName
  550. //
  551. // FUNCTION: PortNumberIsLegal(TCHAR *ptcsPortNumber)
  552. //
  553. // PURPOSE: To determine if the PortNum passed in in the parameter ptcsAddress
  554. // is legal.
  555. //
  556. BOOL CInputChecker::PortNumberIsLegal(TCHAR *ptcsPortNumber)
  557. {
  558. if(IsValidPortNumberInput(ptcsPortNumber, NULL, 0) &&
  559. _tcslen(ptcsPortNumber) >= 1 &&
  560. _tcslen(ptcsPortNumber) <= MAX_PORTNUM_STRING_LENGTH)
  561. {
  562. return TRUE;
  563. }
  564. return FALSE;
  565. } // PortNumberIsLegal
  566. //
  567. // FUNCTION: CommunityNameIsLegal(TCHAR *ptcsCommunityName)
  568. //
  569. // PURPOSE: To determine if the Community Name passed in in the parameter ptcsAddress
  570. // is legal.
  571. //
  572. BOOL CInputChecker::CommunityNameIsLegal(TCHAR *ptcsCommunityName)
  573. {
  574. if(IsValidCommunityNameInput(ptcsCommunityName) &&
  575. _tcslen(ptcsCommunityName) >= 1 &&
  576. _tcslen(ptcsCommunityName) <= MAX_SNMP_COMMUNITY_STR_LEN)
  577. {
  578. return TRUE;
  579. }
  580. return FALSE;
  581. } // CommunityNameIsLegal
  582. //
  583. // FUNCTION: QueueNameIsLegal(TCHAR *ptcsQueueName)
  584. //
  585. // PURPOSE: To determine if the PortNum passed in in the parameter ptcsAddress
  586. // is legal.
  587. //
  588. BOOL CInputChecker::QueueNameIsLegal(TCHAR *ptcsQueueName)
  589. {
  590. if(IsValidQueueNameInput(ptcsQueueName) &&
  591. _tcslen(ptcsQueueName) >= 1 &&
  592. _tcslen(ptcsQueueName) <= MAX_QUEUENAME_LEN)
  593. {
  594. return TRUE;
  595. }
  596. return FALSE;
  597. } // QueueNameIsLegal
  598. //
  599. // FUNCTION: GetAddressType(TCHAR *ptcsAddress)
  600. //
  601. // PURPOSE: To determine if the address passed in in the parameter ptcsAddress
  602. // is an ip address or a host name.
  603. //
  604. AddressType CInputChecker::GetAddressType(TCHAR *ptcsAddress)
  605. {
  606. // determine if we are dealing with a name or an ip address
  607. // if it's an IP Address it will start with a number, otherwise
  608. // it will start with a letter or other character.
  609. if( ptcsAddress[0] == '0' ||
  610. ptcsAddress[0] == '1' ||
  611. ptcsAddress[0] == '2' ||
  612. ptcsAddress[0] == '3' ||
  613. ptcsAddress[0] == '4' ||
  614. ptcsAddress[0] == '5' ||
  615. ptcsAddress[0] == '6' ||
  616. ptcsAddress[0] == '7' ||
  617. ptcsAddress[0] == '8' ||
  618. ptcsAddress[0] == '9')
  619. {
  620. CIPAddress IPAddr;
  621. if (IPAddr.IsValid(ptcsAddress))
  622. return(IPAddress);
  623. else
  624. return (HostName);
  625. }
  626. else
  627. {
  628. return(HostName);
  629. }
  630. } // GetAddressType
  631. //
  632. // FUNCTION: AddressIsLegal(TCHAR *ptcsAddress)
  633. //
  634. // PURPOSE: To determine if the address passed in in the parameter ptcsAddress
  635. // is legal -- That is not too short and either a legal ip address or
  636. // a legal host name.
  637. //
  638. BOOL CInputChecker::AddressIsLegal(TCHAR *ptcsAddress)
  639. {
  640. BOOL bLegalAddress = TRUE;
  641. // determine if the input is at least 2 characters long.
  642. if((_tcslen(ptcsAddress) > 1))
  643. {
  644. if( GetAddressType(ptcsAddress) == IPAddress )
  645. {
  646. CIPAddress IPAddr;
  647. bLegalAddress = IPAddr.IsValid(ptcsAddress);
  648. }
  649. else
  650. {
  651. CHostName HostName(ptcsAddress);
  652. bLegalAddress = HostName.IsValid();
  653. }
  654. }
  655. else
  656. {
  657. bLegalAddress = FALSE;
  658. }
  659. return bLegalAddress;
  660. } // AddressIsLegal
  661. //
  662. // FUNCTION: PortNameIsLegal(TCHAR *ptcsPortName)
  663. //
  664. // PURPOSE: To determine if the PortName passed in in the parameter ptcsPortName
  665. // is legal
  666. //
  667. // Return Value: True if the port name is legal. False if it is not.
  668. //
  669. // Parameters: ptcsPortName - the name of the port to check for legality.
  670. //
  671. BOOL CInputChecker::PortNameIsLegal(TCHAR *ptcsPortName)
  672. {
  673. DWORD dwLen;
  674. dwLen = ptcsPortName && *ptcsPortName ? _tcslen(ptcsPortName) : 0;
  675. //
  676. // Remove trailing spaces
  677. //
  678. while ( dwLen && ptcsPortName[dwLen-1] == ' ' )
  679. --dwLen;
  680. if ( dwLen == 0 )
  681. return FALSE;
  682. ptcsPortName[dwLen] = TEXT('\0');
  683. for ( ; *ptcsPortName ; ++ptcsPortName )
  684. if ( *ptcsPortName == TEXT(',') ||
  685. *ptcsPortName == TEXT('\\') ||
  686. *ptcsPortName == TEXT('/') )
  687. return FALSE;
  688. return TRUE;
  689. } // PortNameIsLegal
  690. //
  691. // FUNCTION: SNMPDevIndexIsLegal(TCHAR *ptcsPortName)
  692. //
  693. // PURPOSE: To determine if the SNMPDevIndex passed in in the parameter psztSNMPDevIndex
  694. // is legal
  695. //
  696. // Return Value: True if the index is legal. False if it is not.
  697. //
  698. // Parameters: psztSNMPDevIndex - the device index to check for legality.
  699. //
  700. BOOL CInputChecker::SNMPDevIndexIsLegal(TCHAR *psztSNMPDevIndex)
  701. {
  702. if((! IsValidDeviceIndexInput(psztSNMPDevIndex)) ||
  703. (_tcslen(psztSNMPDevIndex) < 1))
  704. {
  705. return FALSE;
  706. }
  707. return TRUE;
  708. } // SNMPDevIndexIsLegal
  709. //
  710. // FUNCTION: PortNameIsUnique(TCHAR *ptcsPortName)
  711. //
  712. // PURPOSE: To determine if the PortName passed in in the parameter ptcsPortName
  713. // is Unique.
  714. //
  715. // Return Value: True if the port does not exist. False if it does.
  716. //
  717. // Parameters: psztPortName - the name of the port to check for prior existance.
  718. //
  719. // Note: The spooler must be running on the system in order for this function
  720. // to work properly.
  721. //
  722. BOOL CInputChecker::PortNameIsUnique(TCHAR *ptcsPortName, LPTSTR psztServerName)
  723. {
  724. return(! PortExists(ptcsPortName, psztServerName));
  725. } // PortNameIsUnique
  726. //
  727. // FUNCTION: PortExists()
  728. //
  729. // PURPOSE: Enumerate ports and search for the given port name
  730. //
  731. // Return Value: True if the port exists. False if it does not.
  732. //
  733. // Parameters: psztPortName - the name of the port to check for existance.
  734. // psztServerName - The Name of the server to check on.
  735. //
  736. // Note: The spooler must be running on the system in order for this function
  737. // to work properly.
  738. //
  739. BOOL CInputChecker::PortExists(LPTSTR psztPortName, LPTSTR psztServerName)
  740. {
  741. BOOL Exists = FALSE;
  742. PORT_INFO_1 *pi1 = NULL;
  743. DWORD pcbNeeded = 0;
  744. DWORD pcReturned = 0;
  745. BOOL res = EnumPorts((psztServerName[0] == '\0') ? NULL : psztServerName,
  746. 1, // specifies type of port info structure
  747. (LPBYTE)pi1, // pointer to buffer to receive array of port info. structures
  748. 0, // specifies size, in bytes, of buffer
  749. &pcbNeeded, // pointer to number of bytes stored into buffer (or required buffer size)
  750. &pcReturned // pointer to number of PORT_INFO_*. structures stored into buffer
  751. );
  752. DWORD err = GetLastError();
  753. if(res == 0 && ERROR_INSUFFICIENT_BUFFER == err)
  754. {
  755. pi1 = (PORT_INFO_1 *) malloc(pcbNeeded);
  756. if(pi1 == NULL)
  757. {
  758. pcbNeeded = 0;
  759. }
  760. res = EnumPorts((psztServerName[0] == '\0') ? NULL : psztServerName,
  761. 1,
  762. (LPBYTE)pi1,
  763. pcbNeeded,
  764. &pcbNeeded,
  765. &pcReturned);
  766. for(DWORD i=0;i<pcReturned; i++)
  767. {
  768. if(0 == _tcsicmp(pi1[i].pName, psztPortName))
  769. {
  770. Exists = TRUE;
  771. break;
  772. }
  773. }
  774. }
  775. if(pi1 != NULL)
  776. {
  777. free(pi1);
  778. pi1 = NULL;
  779. }
  780. return(Exists);
  781. } // PortExists