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.

443 lines
16 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. tgroup.c
  5. Abstract:
  6. Test for cluster network API
  7. Author:
  8. John Vert (jvert) 15-Mar-1996
  9. Charlie Wickham (charlwi) 6-Jun-1997
  10. Revision History:
  11. --*/
  12. #include "windows.h"
  13. #include "cluster.h"
  14. #include "stdio.h"
  15. #include "stdlib.h"
  16. int
  17. _cdecl
  18. main (argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22. HCLUSTER Cluster;
  23. HNETWORK Network;
  24. HNODE Node;
  25. HCLUSENUM ResEnum;
  26. DWORD ClusterCountBefore, ClusterCountAfter;
  27. HNETWORKENUM hNetworkEnum;
  28. DWORD NetCountBefore, NetCountAfter;
  29. HNODEENUM hNodeEnum;
  30. DWORD NodeCountBefore, NodeCountAfter;
  31. DWORD i,j;
  32. DWORD Status;
  33. WCHAR NameBuf[50];
  34. DWORD NameLen;
  35. DWORD Type;
  36. CLUSTER_NETWORK_STATE NetworkState;
  37. CLUSTER_NODE_STATE NodeState;
  38. DWORD Count;
  39. Cluster = OpenCluster(NULL);
  40. if (Cluster == NULL) {
  41. fprintf(stderr, "OpenCluster(NULL) failed %d\n",GetLastError());
  42. return(0);
  43. }
  44. //
  45. // Dump nodes
  46. //
  47. printf("\n\nENUMERATING NODES\n");
  48. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_NODE);
  49. if (ResEnum == NULL) {
  50. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  51. return(0);
  52. }
  53. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  54. for(i=0; ; i++) {
  55. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  56. Status = ClusterEnum(ResEnum,
  57. i,
  58. &Type,
  59. NameBuf,
  60. &NameLen);
  61. if (Status == ERROR_NO_MORE_ITEMS) {
  62. break;
  63. } else if (Status != ERROR_SUCCESS) {
  64. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  65. return(0);
  66. }
  67. if (Type != CLUSTER_ENUM_NODE) {
  68. printf("Invalid Type %d returned from ClusterEnum\n", Type);
  69. return(0);
  70. }
  71. fprintf(stderr, "\nOpening Node: %ws\n", NameBuf);
  72. Node = OpenClusterNode(Cluster, NameBuf);
  73. if (Node == NULL) {
  74. fprintf(stderr, "OpenClusterNode %ws failed %d\n",NameBuf, GetLastError());
  75. return(0);
  76. }
  77. NodeState = GetClusterNodeState(Node);
  78. if (NodeState == -1) {
  79. Status = GetLastError();
  80. if ( Status != ERROR_SUCCESS )
  81. fprintf(stderr, "GetClusterNodeState failed %d\n", Status);
  82. }
  83. if (NodeState == ClusterNodeUp) {
  84. printf("Node %ws is UP\n", NameBuf);
  85. } else if (NodeState == ClusterNodeDown) {
  86. printf("Node %ws is DOWN\n", NameBuf );
  87. }
  88. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  89. Status = GetClusterNodeId( Node, NameBuf, &NameLen );
  90. if (Status == ERROR_SUCCESS) {
  91. fprintf(stderr, "Node ID: %ws\n", NameBuf );
  92. } else {
  93. Status = GetLastError();
  94. fprintf(stderr, "GetClusterNodeId failed %d\n", Status);
  95. }
  96. hNodeEnum = ClusterNodeOpenEnum(Node,
  97. CLUSTER_NODE_ENUM_NETINTERFACES);
  98. if (hNodeEnum == NULL) {
  99. fprintf(stderr, "Node %ws failed to open enum %d\n",NameBuf, GetLastError());
  100. } else {
  101. NodeCountBefore = ClusterNodeGetEnumCount(hNodeEnum);
  102. for (j=0; ; j++) {
  103. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  104. Status = ClusterNodeEnum(hNodeEnum,
  105. j,
  106. &Type,
  107. NameBuf,
  108. &NameLen);
  109. if (Status == ERROR_NO_MORE_ITEMS) {
  110. break;
  111. } else if (Status != ERROR_SUCCESS) {
  112. fprintf(stderr, "Failed to enum node item %d, error %d\n",j,Status);
  113. }
  114. if (Type != CLUSTER_NODE_ENUM_NETINTERFACES) {
  115. printf("Invalid Type %d returned from ClusterNodeEnum\n", Type);
  116. return(0);
  117. }
  118. printf("\tInterface %ws\n",NameBuf);
  119. }
  120. if (Status == ERROR_NO_MORE_ITEMS) {
  121. printf("\nNode count: %d\n", j);
  122. NodeCountAfter = ClusterNodeGetEnumCount(hNodeEnum);
  123. if (NodeCountBefore != NodeCountAfter)
  124. fprintf(stderr, "\nReported node count was %d before enumeration, and %d afterward\n", NodeCountBefore, NodeCountAfter);
  125. else if (j != NodeCountBefore)
  126. fprintf(stderr, "\nReported node count: %d\n", NodeCountBefore);
  127. }
  128. ClusterNodeCloseEnum(hNodeEnum);
  129. }
  130. CloseClusterNode(Node);
  131. }
  132. if (Status == ERROR_NO_MORE_ITEMS) {
  133. printf("\nCluster count: %d\n", i);
  134. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  135. if (ClusterCountBefore != ClusterCountAfter)
  136. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  137. else if (i != ClusterCountBefore)
  138. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  139. }
  140. ClusterCloseEnum(ResEnum);
  141. //
  142. // Dump networks
  143. //
  144. printf("\n\nENUMERATING NETWORKS\n");
  145. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_NETWORK);
  146. if (ResEnum == NULL) {
  147. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  148. return(0);
  149. }
  150. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  151. for(i=0; ; i++) {
  152. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  153. Status = ClusterEnum(ResEnum,
  154. i,
  155. &Type,
  156. NameBuf,
  157. &NameLen);
  158. if (Status == ERROR_NO_MORE_ITEMS) {
  159. break;
  160. } else if (Status != ERROR_SUCCESS) {
  161. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  162. return(0);
  163. }
  164. if (Type != CLUSTER_ENUM_NETWORK) {
  165. printf("Invalid Type %d returned from ClusterEnum\n", Type);
  166. return(0);
  167. }
  168. fprintf(stderr, "\nOpening Network: %ws\n", NameBuf);
  169. Network = OpenClusterNetwork(Cluster, NameBuf);
  170. if (Network == NULL) {
  171. fprintf(stderr, "OpenClusterNetwork %ws failed %d\n",NameBuf, GetLastError());
  172. return(0);
  173. }
  174. NetworkState = GetClusterNetworkState(Network);
  175. if (NetworkState == -1) {
  176. Status = GetLastError();
  177. if ( Status != ERROR_SUCCESS )
  178. fprintf(stderr, "GetClusterNetworkState failed %d\n", Status);
  179. }
  180. if (NetworkState == ClusterNetworkUp) {
  181. printf("Network %ws is UP\n", NameBuf);
  182. } else if (NetworkState == ClusterNetworkPartitioned) {
  183. printf("Network %ws is PARTITIONED\n", NameBuf );
  184. } else if (NetworkState == ClusterNetworkDown) {
  185. printf("Network %ws is DOWN\n", NameBuf );
  186. } else if (NetworkState == ClusterNetworkStateUnknown) {
  187. printf("Network %ws is UNKNOWN\n", NameBuf );
  188. } else if (NetworkState == ClusterNetworkUnavailable) {
  189. printf("Network %ws is UNAVAILABLE\n", NameBuf );
  190. }
  191. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  192. Status = GetClusterNetworkId( Network, NameBuf, &NameLen );
  193. if (Status == ERROR_SUCCESS) {
  194. fprintf(stderr, "Network ID: %ws\n", NameBuf );
  195. } else {
  196. Status = GetLastError();
  197. fprintf(stderr, "GetClusterNetworkId failed %d\n", Status);
  198. }
  199. hNetworkEnum = ClusterNetworkOpenEnum(Network,
  200. CLUSTER_NETWORK_ENUM_NETINTERFACES);
  201. if (hNetworkEnum == NULL) {
  202. fprintf(stderr, "Network %ws failed to open enum %d\n",NameBuf, GetLastError());
  203. } else {
  204. NetCountBefore = ClusterNetworkGetEnumCount(hNetworkEnum);
  205. for (j=0; ; j++) {
  206. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  207. Status = ClusterNetworkEnum(hNetworkEnum,
  208. j,
  209. &Type,
  210. NameBuf,
  211. &NameLen);
  212. if (Status == ERROR_NO_MORE_ITEMS) {
  213. break;
  214. } else if (Status != ERROR_SUCCESS) {
  215. fprintf(stderr, "Failed to enum network item %d, error %d\n",j,Status);
  216. }
  217. if (Type != CLUSTER_NETWORK_ENUM_NETINTERFACES) {
  218. printf("Invalid Type %d returned from ClusterNetworkEnum\n", Type);
  219. return(0);
  220. }
  221. printf("\tInterface %ws\n",NameBuf);
  222. }
  223. if (Status == ERROR_NO_MORE_ITEMS) {
  224. printf("\nNetwork count: %d\n", j);
  225. NetCountAfter = ClusterNetworkGetEnumCount(hNetworkEnum);
  226. if (NetCountBefore != NetCountAfter)
  227. fprintf(stderr, "\nReported network count was %d before enumeration, and %d afterward\n", NetCountBefore, NetCountAfter);
  228. else if (j != NetCountBefore)
  229. fprintf(stderr, "\nReported network count: %d\n", NetCountBefore);
  230. }
  231. ClusterNetworkCloseEnum(hNetworkEnum);
  232. }
  233. CloseClusterNetwork(Network);
  234. }
  235. if (Status == ERROR_NO_MORE_ITEMS) {
  236. printf("\nCluster count: %d\n", i);
  237. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  238. if (ClusterCountBefore != ClusterCountAfter)
  239. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  240. else if (i != ClusterCountBefore)
  241. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  242. }
  243. ClusterCloseEnum(ResEnum);
  244. printf("\n\nCURRENT INTERNAL NETWORK PRIORITY ORDER\n");
  245. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_INTERNAL_NETWORK);
  246. if (ResEnum == NULL) {
  247. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  248. return(0);
  249. }
  250. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  251. for(i=0, Count=0; ; i++) {
  252. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  253. Status = ClusterEnum(ResEnum,
  254. i,
  255. &Type,
  256. NameBuf,
  257. &NameLen);
  258. if (Status == ERROR_NO_MORE_ITEMS) {
  259. break;
  260. } else if (Status != ERROR_SUCCESS) {
  261. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  262. return(0);
  263. }
  264. printf("%ws\n", NameBuf);
  265. Count++;
  266. }
  267. if (Status == ERROR_NO_MORE_ITEMS) {
  268. printf("\nCluster count: %d\n", i);
  269. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  270. if (ClusterCountBefore != ClusterCountAfter)
  271. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  272. else if (i != ClusterCountBefore)
  273. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  274. }
  275. ClusterCloseEnum(ResEnum);
  276. if (Count != 0) {
  277. DWORD newIndex;
  278. HNETWORK * NetworkList = LocalAlloc(
  279. LMEM_FIXED | LMEM_ZEROINIT,
  280. sizeof(HNETWORK) * Count
  281. );
  282. if (NetworkList == NULL) {
  283. fprintf(stderr, "Out of memory\n");
  284. return(0);
  285. }
  286. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_INTERNAL_NETWORK);
  287. if (ResEnum == NULL) {
  288. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  289. return(0);
  290. }
  291. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  292. for(i=0; ; i++) {
  293. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  294. Status = ClusterEnum(ResEnum,
  295. i,
  296. &Type,
  297. NameBuf,
  298. &NameLen);
  299. if (Status == ERROR_NO_MORE_ITEMS) {
  300. break;
  301. } else if (Status != ERROR_SUCCESS) {
  302. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  303. return(0);
  304. }
  305. if (i == Count) {
  306. fprintf(stderr, "Another network was added after we counted.\n");
  307. return(0);
  308. }
  309. newIndex = (i+1) % Count;
  310. NetworkList[newIndex] = OpenClusterNetwork(Cluster, NameBuf);
  311. if (NetworkList[newIndex] == NULL) {
  312. fprintf(stderr, "OpenClusterNetwork %ws failed %d\n",NameBuf, GetLastError());
  313. return(0);
  314. }
  315. }
  316. if (Status == ERROR_NO_MORE_ITEMS) {
  317. printf("\nCluster count: %d\n", i);
  318. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  319. if (ClusterCountBefore != ClusterCountAfter)
  320. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  321. else if (i != ClusterCountBefore)
  322. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  323. }
  324. ClusterCloseEnum(ResEnum);
  325. Status = SetClusterNetworkPriorityOrder(
  326. Cluster,
  327. Count,
  328. NetworkList
  329. );
  330. if (Status != ERROR_SUCCESS) {
  331. fprintf(stderr, "SetClusterNetworkPriorityOrder failed %d\n", Status);
  332. return(0);
  333. }
  334. for (i=0; i<Count; i++) {
  335. if (NetworkList[i] != NULL) {
  336. CloseClusterNetwork(NetworkList[i]);
  337. }
  338. }
  339. printf("\n\nNEW INTERNAL NETWORK PRIORITY ORDER\n");
  340. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_INTERNAL_NETWORK);
  341. if (ResEnum == NULL) {
  342. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  343. return(0);
  344. }
  345. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  346. for(i=0; ; i++) {
  347. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  348. Status = ClusterEnum(ResEnum,
  349. i,
  350. &Type,
  351. NameBuf,
  352. &NameLen);
  353. if (Status == ERROR_NO_MORE_ITEMS) {
  354. break;
  355. } else if (Status != ERROR_SUCCESS) {
  356. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  357. return(0);
  358. }
  359. printf("%ws\n", NameBuf);
  360. }
  361. if (Status == ERROR_NO_MORE_ITEMS) {
  362. printf("\nCluster count: %d\n", i);
  363. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  364. if (ClusterCountBefore != ClusterCountAfter)
  365. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  366. else if (i != ClusterCountBefore)
  367. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  368. }
  369. ClusterCloseEnum(ResEnum);
  370. }
  371. CloseCluster(Cluster);
  372. }