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.

615 lines
14 KiB

  1. #ifndef _UTILS_H
  2. #define _UTILS_H
  3. //
  4. // Copyright (c) Microsoft. All Rights Reserved
  5. //
  6. // THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Microsoft.
  7. // The copyright notice above does not evidence any
  8. // actual or intended publication of such source code.
  9. //
  10. // OneLiner : common include file.
  11. // DevUnit : wlbstest
  12. // Author : Murtaza Hakim
  13. //
  14. // Description:
  15. // -----------
  16. // History:
  17. // --------
  18. //
  19. //
  20. // Revised by : mhakim
  21. // Date : 02-12-01
  22. // Reason : added password to clusterproperties.
  23. #include <vector>
  24. using namespace std;
  25. struct ClusterProperties
  26. {
  27. // default constructor
  28. ClusterProperties();
  29. // Equality operator
  30. bool
  31. operator==( const ClusterProperties& objToCompare );
  32. // inequality operator
  33. bool
  34. operator!=( const ClusterProperties& objToCompare );
  35. bool HaveClusterPropertiesChanged( const ClusterProperties& objToCompare,
  36. bool *pbOnlyClusterNameChanged,
  37. bool *pbClusterIpChanged);
  38. _bstr_t cIP; // Primary IP address.
  39. _bstr_t cSubnetMask; // Subnet mask.
  40. _bstr_t cFullInternetName; // Full Internet name.
  41. _bstr_t cNetworkAddress; // Network address.
  42. bool multicastSupportEnabled; // Multicast support.
  43. bool remoteControlEnabled; // Remote control.
  44. // Edited (mhakim 12-02-01)
  45. // password may be required to be set.
  46. // but note that it cannot be got from an existing cluster.
  47. _bstr_t password; // Remote control password.
  48. // for whistler
  49. bool igmpSupportEnabled; // igmp support
  50. bool clusterIPToMulticastIP; // indicates whether to use cluster ip or user provided ip.
  51. _bstr_t multicastIPAddress; // user provided multicast ip.
  52. long igmpJoinInterval; // user provided multicast ip.
  53. };
  54. struct HostProperties
  55. {
  56. // default constructor
  57. HostProperties();
  58. // Equality operator
  59. bool
  60. operator==( const HostProperties& objToCompare );
  61. // inequality operator
  62. bool
  63. operator!=( const HostProperties& objToCompare );
  64. _bstr_t hIP; // Dedicated IP Address.
  65. _bstr_t hSubnetMask; // Subnet mask.
  66. long hID; // Priority(Unique host ID).
  67. bool initialClusterStateActive; // Initial Cluster State.
  68. _bstr_t machineName; // machine name.
  69. };
  70. class Common
  71. {
  72. public:
  73. enum
  74. {
  75. BUF_SIZE = 1000,
  76. ALL_PORTS = 0xffffffff,
  77. ALL_HOSTS = 100,
  78. THIS_HOST = 0,
  79. };
  80. };
  81. class CommonUtils
  82. {
  83. public:
  84. // converts the CIPAddressCtrl embedded ip into
  85. // dotted decimal string representation.
  86. static
  87. _bstr_t
  88. getCIPAddressCtrlString( CIPAddressCtrl& ip );
  89. // fills the CIPAddressCtrl with the dotted decimal
  90. // string representation.
  91. static
  92. void
  93. fillCIPAddressCtrlString( CIPAddressCtrl& ip,
  94. const _bstr_t& ipAdddress );
  95. static
  96. void
  97. getVectorFromSafeArray( SAFEARRAY*& stringArray,
  98. vector<_bstr_t>& strings );
  99. static
  100. void
  101. getSafeArrayFromVector( const vector<_bstr_t>& strings,
  102. SAFEARRAY*& stringArray
  103. );
  104. private:
  105. enum
  106. {
  107. BUF_SIZE = 1000,
  108. };
  109. };
  110. // typedefs for _com_ptr_t
  111. _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
  112. _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
  113. _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
  114. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
  115. _COM_SMARTPTR_TYPEDEF(IWbemCallResult, __uuidof(IWbemCallResult));
  116. _COM_SMARTPTR_TYPEDEF(IWbemStatusCodeText, __uuidof(IWbemStatusCodeText));
  117. #define NLBMGR_USERNAME (const BSTR) NULL
  118. #define NLBMGR_PASSWORD (const BSTR) NULL
  119. void
  120. GetErrorCodeText(WBEMSTATUS wStat , _bstr_t& errText );
  121. // Class Definition
  122. class MIPAddress
  123. {
  124. public:
  125. enum IPClass
  126. {
  127. classA,
  128. classB,
  129. classC,
  130. classD,
  131. classE
  132. };
  133. // Description
  134. // -----------
  135. // Checks if the ip address supplied is valid.
  136. //
  137. // IP address needs to be in dotted decimal
  138. // for eg. 192.31.56.2, 128.1.1.1, 1.1.1.1 etc.
  139. // ip addresses in the form 192.31 are not allowed.
  140. // There must be exactly four parts.
  141. //
  142. //
  143. // Parameters
  144. // ----------
  145. // ipAddrToCheck in : ipAddr to check in dotted dec notation.
  146. //
  147. // Returns
  148. // -------
  149. // true if valid else false.
  150. static
  151. bool
  152. checkIfValid(const _bstr_t& ipAddrToCheck );
  153. // Description
  154. // -----------
  155. // Gets the default subnet mask for ip address. The ip address
  156. // needs to be valid for operation to be successful.
  157. //
  158. // IP address needs to be in dotted decimal
  159. // for eg. 192.31.56.2, 128.1.1.1, 1.1.1.1 etc.
  160. // ip addresses in the form 192.31 are not allowed.
  161. // There must be exactly four parts.
  162. //
  163. // Parameters
  164. // ----------
  165. // ipAddress IN : ip address for which default subnet required.
  166. // subnetMask OUT : default subnet mask for ip.
  167. //
  168. // Returns
  169. // -------
  170. // true if able to find default subnet or false if ipAddress was
  171. // invalid.
  172. static
  173. bool
  174. getDefaultSubnetMask( const _bstr_t& ipAddr,
  175. _bstr_t& subnetMask );
  176. // Description
  177. // -----------
  178. // Gets the class to which this ip address belongs.
  179. // class A: 1 - 126
  180. // class B: 128 - 191
  181. // class C: 192 - 223
  182. // class D: 224 - 239
  183. // class D: 240 - 247
  184. //
  185. // IP address needs to be in dotted decimal
  186. // for eg. 192.31.56.2, 128.1.1.1, 1.1.1.1 etc.
  187. // ip addresses in the form 192.31 are not allowed.
  188. // There must be exactly four parts.
  189. //
  190. // Parameters
  191. // ----------
  192. // ipAddress IN : ip address for which class is to be found.
  193. // ipClass OUT : class to which ip belongs.
  194. //
  195. // Returns
  196. // -------
  197. // true if able to find class or false if not able to find.
  198. //
  199. static
  200. bool
  201. getIPClass( const _bstr_t& ipAddr,
  202. IPClass& ipClass );
  203. static
  204. bool
  205. isValidIPAddressSubnetMaskPair( const _bstr_t& ipAddress,
  206. const _bstr_t& subnetMask );
  207. static
  208. bool
  209. isContiguousSubnetMask( const _bstr_t& subnetMask );
  210. private:
  211. };
  212. //------------------------------------------------------
  213. // Ensure Type Safety
  214. //------------------------------------------------------
  215. typedef class MIPAddress MIPAddress;
  216. class MUsingCom
  217. {
  218. public:
  219. enum MUsingCom_Error
  220. {
  221. MUsingCom_SUCCESS = 0,
  222. COM_FAILURE = 1,
  223. };
  224. // constructor
  225. MUsingCom( DWORD type = COINIT_DISABLE_OLE1DDE | COINIT_MULTITHREADED );
  226. // destructor
  227. ~MUsingCom();
  228. //
  229. MUsingCom_Error
  230. getStatus();
  231. private:
  232. MUsingCom_Error status;
  233. };
  234. class ResourceString
  235. {
  236. public:
  237. static
  238. ResourceString*
  239. Instance();
  240. static
  241. const _bstr_t&
  242. GetIDString( UINT id );
  243. protected:
  244. private:
  245. static map< UINT, _bstr_t> resourceStrings;
  246. static ResourceString* _instance;
  247. };
  248. // helper functions
  249. const _bstr_t&
  250. GETRESOURCEIDSTRING( UINT id );
  251. #include <vector>
  252. using namespace std;
  253. //
  254. //------------------------------------------------------
  255. //
  256. //------------------------------------------------------
  257. // External References
  258. //------------------------------------------------------
  259. //
  260. //------------------------------------------------------
  261. // Constant Definitions
  262. //
  263. //------------------------------------------------------
  264. class WTokens
  265. {
  266. public:
  267. //
  268. //
  269. // data
  270. // none
  271. //
  272. // constructor
  273. //------------------------------------------------------
  274. // Description
  275. // -----------
  276. // constructor
  277. //
  278. // Returns
  279. // -------
  280. // none.
  281. //
  282. //------------------------------------------------------
  283. WTokens(
  284. wstring strToken, // IN: Wstring to tokenize.
  285. wstring strDelimit ); // IN: Delimiter.
  286. //
  287. //------------------------------------------------------
  288. // Description
  289. // -----------
  290. // Default constructor
  291. //
  292. // Returns
  293. // -------
  294. // none.
  295. //
  296. //------------------------------------------------------
  297. WTokens();
  298. //
  299. // destructor
  300. //------------------------------------------------------
  301. // Description
  302. // -----------
  303. // destructor
  304. //
  305. // Returns
  306. // -------
  307. // none.
  308. //------------------------------------------------------
  309. ~WTokens();
  310. //
  311. // member functions
  312. //------------------------------------------------------
  313. // Description
  314. // -----------
  315. //
  316. // Returns
  317. // -------
  318. // The tokens.
  319. //------------------------------------------------------
  320. vector<wstring>
  321. tokenize();
  322. //
  323. //------------------------------------------------------
  324. // Description
  325. // -----------
  326. // constructor
  327. //
  328. // Returns
  329. // -------
  330. // none.
  331. //
  332. //------------------------------------------------------
  333. void
  334. init(
  335. wstring strToken, // IN: Wstring to tokenize.
  336. wstring strDelimit ); // IN: Delimiter.
  337. //
  338. protected:
  339. // Data
  340. // none
  341. //
  342. // Constructors
  343. // none
  344. //
  345. // Destructor
  346. // none
  347. //
  348. // Member Functions
  349. // none
  350. //
  351. private:
  352. //
  353. /// Data
  354. wstring _strToken;
  355. wstring _strDelimit;
  356. //
  357. /// Constructors
  358. /// none
  359. //
  360. /// Destructor
  361. /// none
  362. //
  363. /// Member Functions
  364. /// none
  365. //
  366. };
  367. HKEY
  368. NlbMgrRegCreateKey(
  369. LPCWSTR szSubKey // Optional
  370. );
  371. UINT
  372. NlbMgrRegReadUINT(
  373. HKEY hKey,
  374. LPCWSTR szName,
  375. UINT Default
  376. );
  377. VOID
  378. NlbMgrRegWriteUINT(
  379. HKEY hKey,
  380. LPCWSTR szName,
  381. UINT Value
  382. );
  383. void
  384. GetTimeAndDate(_bstr_t &bstrTime, _bstr_t &bstrDate);
  385. //
  386. //------------------------------------------------------
  387. // Inline Functions
  388. //------------------------------------------------------
  389. //
  390. //------------------------------------------------------
  391. // Ensure Type Safety
  392. //------------------------------------------------------
  393. typedef class WTokens WTokens;
  394. //
  395. // Used for maintaining a log on the stack.
  396. // Usage is NOT thread-safe -- each instance must be used
  397. // by a single thread.
  398. //
  399. class CLocalLogger
  400. {
  401. public:
  402. CLocalLogger(VOID)
  403. : m_pszLog (NULL), m_LogSize(0), m_CurrentOffset(0)
  404. {
  405. m_Empty[0] = 0; // The empty string.
  406. }
  407. ~CLocalLogger()
  408. {
  409. delete[] m_pszLog;
  410. m_pszLog=NULL;
  411. }
  412. VOID
  413. LogString(
  414. LPCWSTR szStr
  415. );
  416. VOID
  417. Log(
  418. IN UINT ResourceID,
  419. ...
  420. );
  421. VOID
  422. ExtractLog(OUT LPCWSTR &pLog, UINT &Size)
  423. //
  424. // pLog -- set to pointer to internal buffer if there is stuff in the
  425. // log, otherwise NULL.
  426. //
  427. // Size -- in chars; includes ending NULL
  428. //
  429. {
  430. if (m_CurrentOffset != 0)
  431. {
  432. pLog = m_pszLog;
  433. Size = m_CurrentOffset+1; // + 1 for ending NULL.
  434. }
  435. else
  436. {
  437. pLog = NULL;
  438. Size = 0;
  439. }
  440. }
  441. LPCWSTR
  442. GetStringSafe(void)
  443. {
  444. LPCWSTR szLog = NULL;
  445. UINT Size;
  446. ExtractLog(REF szLog, REF Size);
  447. if (szLog == NULL)
  448. {
  449. //
  450. // Replace NULL by a pointer to an empty string.
  451. //
  452. szLog = m_Empty;
  453. }
  454. return szLog;
  455. }
  456. private:
  457. WCHAR *m_pszLog;
  458. UINT m_LogSize; // Current size of the log.
  459. UINT m_CurrentOffset; // Characters left in the log.
  460. WCHAR m_Empty[1]; // The empty string.
  461. };
  462. NLBERROR
  463. AnalyzeNlbConfiguration(
  464. IN const NLB_EXTENDED_CLUSTER_CONFIGURATION &Cfg,
  465. IN OUT CLocalLogger &logErrors
  466. );
  467. //
  468. // logErrors - a log of config errors
  469. //
  470. NLBERROR
  471. AnalyzeNlbConfigurationPair(
  472. IN const NLB_EXTENDED_CLUSTER_CONFIGURATION &Cfg,
  473. IN const NLB_EXTENDED_CLUSTER_CONFIGURATION &OtherCfg,
  474. IN BOOL fOtherIsCluster,
  475. IN BOOL fCheckOtherForConsistancy,
  476. OUT BOOL &fConnectivityChange,
  477. IN OUT CLocalLogger &logErrors,
  478. IN OUT CLocalLogger &logDifferences
  479. );
  480. //
  481. // logErrors - a log of config errors
  482. // logDifferences - a log of differences between
  483. // Cfg and UpOtherCfg.
  484. // fCheckOtherForConsistancy -- if true, we will check Cfg
  485. // against pOtherCfg. If fOtherIsCluster, we expect
  486. // cluster wide properties to match, else we expect
  487. // cluster-wide properties to match as well as host-specific
  488. // properteis to not conflict.
  489. //
  490. //
  491. // Processes the windows and afx msg loops.
  492. //
  493. void
  494. ProcessMsgQueue(void);
  495. //
  496. // Max length in chars, and including ending NULL, for an encrypted
  497. // password.
  498. //
  499. #define MAX_ENCRYPTED_PASSWORD_LENGTH \
  500. (2*sizeof(WCHAR)*(CREDUI_MAX_PASSWORD_LENGTH+1))
  501. BOOL
  502. PromptForEncryptedCreds(
  503. IN HWND hWnd,
  504. IN LPCWSTR szCaptionText,
  505. IN LPCWSTR szMessageText,
  506. IN OUT LPWSTR szUserName,
  507. IN UINT cchUserName,
  508. IN OUT LPWSTR szPassword, // encrypted password
  509. IN UINT cchPassword // size of szPassword
  510. );
  511. #endif // _UTILS_H