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.

774 lines
20 KiB

  1. #include "allinc.h"
  2. DWORD
  3. LoadSystem()
  4. {
  5. DWORD dwResult;
  6. TraceEnter("LoadSystem");
  7. if(g_Cache.pRpcSysInfo)
  8. {
  9. if(g_Cache.pRpcSysInfo->aaSysObjectID.asnValue.object.ids)
  10. {
  11. SnmpUtilOidFree(&g_Cache.pRpcSysInfo->aaSysObjectID.asnValue.object);
  12. }
  13. HeapFree(g_hPrivateHeap,
  14. 0,
  15. g_Cache.pRpcSysInfo);
  16. g_Cache.pRpcSysInfo = NULL;
  17. }
  18. dwResult = GetSysInfo(&(g_Cache.pRpcSysInfo),
  19. g_hPrivateHeap,
  20. 0);
  21. if(dwResult isnot NO_ERROR)
  22. {
  23. TRACE1("GetSysInfo failed with error %x",dwResult);
  24. TraceLeave("LoadSysInfo");
  25. return dwResult;
  26. }
  27. TraceLeave("LoadSysInfo");
  28. return NO_ERROR;
  29. }
  30. DWORD
  31. LoadIfTable()
  32. {
  33. DWORD dwResult;
  34. TraceEnter("LoadIfTable");
  35. if(g_Cache.pRpcIfTable)
  36. {
  37. HeapFree(g_hPrivateHeap,
  38. 0,
  39. g_Cache.pRpcIfTable);
  40. g_Cache.pRpcIfTable = NULL;
  41. }
  42. dwResult = InternalGetIfTable(&(g_Cache.pRpcIfTable),
  43. g_hPrivateHeap,
  44. 0);
  45. if(dwResult isnot NO_ERROR)
  46. {
  47. TRACE1("GetIfTable failed with error %x",
  48. dwResult);
  49. TraceLeave("LoadIfTable");
  50. return dwResult;
  51. }
  52. TraceLeave("LoadIfTable");
  53. return NO_ERROR;
  54. }
  55. DWORD
  56. LoadIpAddrTable()
  57. {
  58. DWORD dwResult;
  59. TraceEnter("LoadIpAddrTable");
  60. if(g_Cache.pRpcIpAddrTable)
  61. {
  62. HeapFree(g_hPrivateHeap,
  63. 0,
  64. g_Cache.pRpcIpAddrTable);
  65. g_Cache.pRpcIpAddrTable = NULL;
  66. }
  67. dwResult = InternalGetIpAddrTable(&(g_Cache.pRpcIpAddrTable),
  68. g_hPrivateHeap,
  69. 0);
  70. if(dwResult isnot NO_ERROR)
  71. {
  72. TRACE1("GetIpAddrTable failed with error %x",
  73. dwResult);
  74. TraceLeave("LoadIpAddrTable");
  75. return dwResult;
  76. }
  77. TraceLeave("LoadIpAddrTable");
  78. return NO_ERROR;
  79. }
  80. DWORD
  81. LoadIpNetTable()
  82. {
  83. DWORD dwResult;
  84. TraceEnter("LoadIpNetTable");
  85. if(g_Cache.pRpcIpNetTable)
  86. {
  87. HeapFree(g_hPrivateHeap,
  88. 0,
  89. g_Cache.pRpcIpNetTable);
  90. g_Cache.pRpcIpNetTable = NULL;
  91. }
  92. dwResult = InternalGetIpNetTable(&(g_Cache.pRpcIpNetTable),
  93. g_hPrivateHeap,
  94. 0);
  95. if(dwResult isnot NO_ERROR)
  96. {
  97. TRACE1("GetIpNetTable failed with error %x",
  98. dwResult);
  99. TraceLeave("LoadIpNetTable");
  100. return dwResult;
  101. }
  102. TraceLeave("LoadIpNetTable");
  103. return NO_ERROR;
  104. }
  105. DWORD
  106. LoadIpForwardTable()
  107. {
  108. DWORD dwResult;
  109. TraceEnter("LoadIpForwardTable");
  110. if(g_Cache.pRpcIpForwardTable)
  111. {
  112. HeapFree(g_hPrivateHeap,
  113. 0,
  114. g_Cache.pRpcIpForwardTable);
  115. g_Cache.pRpcIpForwardTable = NULL;
  116. }
  117. dwResult = InternalGetIpForwardTable(&(g_Cache.pRpcIpForwardTable),
  118. g_hPrivateHeap,
  119. 0);
  120. if(dwResult isnot NO_ERROR)
  121. {
  122. TRACE1("GetIpForwardTable failed with error %x",
  123. dwResult);
  124. TraceLeave("LoadIpForwardTable");
  125. return dwResult;
  126. }
  127. TraceLeave("LoadIpForwardTable");
  128. return NO_ERROR;
  129. }
  130. DWORD
  131. LoadTcpTable()
  132. {
  133. DWORD dwResult, i;
  134. TraceEnter("LoadTcpTable");
  135. if(g_Cache.pRpcTcpTable)
  136. {
  137. HeapFree(g_hPrivateHeap,
  138. 0,
  139. g_Cache.pRpcTcpTable);
  140. g_Cache.pRpcTcpTable = NULL;
  141. }
  142. dwResult = InternalGetTcpTable(&(g_Cache.pRpcTcpTable),
  143. g_hPrivateHeap,
  144. 0);
  145. if(dwResult isnot NO_ERROR)
  146. {
  147. TRACE1("GetTcpTable failed with error %x",
  148. dwResult);
  149. TraceLeave("LoadTcpTable");
  150. return dwResult;
  151. }
  152. //
  153. // modify port numbers to be in host byte order
  154. //
  155. for (i = 0; i < g_Cache.pRpcTcpTable->dwNumEntries; i++)
  156. {
  157. g_Cache.pRpcTcpTable->table[i].dwLocalPort =
  158. (DWORD)ntohs((WORD)g_Cache.pRpcTcpTable->table[i].dwLocalPort);
  159. g_Cache.pRpcTcpTable->table[i].dwRemotePort =
  160. (DWORD)ntohs((WORD)g_Cache.pRpcTcpTable->table[i].dwRemotePort);
  161. }
  162. TraceLeave("LoadTcpTable");
  163. return NO_ERROR;
  164. }
  165. DWORD
  166. LoadTcp6Table()
  167. {
  168. DWORD dwResult, i;
  169. TraceEnter("LoadTcp6Table");
  170. if(g_Cache.pRpcTcp6Table)
  171. {
  172. HeapFree(g_hPrivateHeap,
  173. 0,
  174. g_Cache.pRpcTcp6Table);
  175. g_Cache.pRpcTcp6Table = NULL;
  176. }
  177. dwResult = AllocateAndGetTcpExTableFromStack(
  178. (TCP_EX_TABLE **)&(g_Cache.pRpcTcp6Table),
  179. TRUE, g_hPrivateHeap, 0, AF_INET6);
  180. if(dwResult isnot NO_ERROR)
  181. {
  182. TRACE1("GetTcp6Table failed with error %x",
  183. dwResult);
  184. TraceLeave("LoadTcp6Table");
  185. return dwResult;
  186. }
  187. //
  188. // modify port numbers to be in host byte order
  189. // and scope ids to be in network byte order
  190. //
  191. for (i = 0; i < g_Cache.pRpcTcp6Table->dwNumEntries; i++)
  192. {
  193. g_Cache.pRpcTcp6Table->table[i].tct_localport =
  194. (DWORD)ntohs((WORD)g_Cache.pRpcTcp6Table->table[i].tct_localport);
  195. g_Cache.pRpcTcp6Table->table[i].tct_remoteport =
  196. (DWORD)ntohs((WORD)g_Cache.pRpcTcp6Table->table[i].tct_remoteport);
  197. g_Cache.pRpcTcp6Table->table[i].tct_localscopeid =
  198. htonl(g_Cache.pRpcTcp6Table->table[i].tct_localscopeid);
  199. g_Cache.pRpcTcp6Table->table[i].tct_remotescopeid =
  200. htonl(g_Cache.pRpcTcp6Table->table[i].tct_remotescopeid);
  201. }
  202. TraceLeave("LoadTcp6Table");
  203. return NO_ERROR;
  204. }
  205. DWORD
  206. LoadUdpTable()
  207. {
  208. DWORD dwResult, i;
  209. TraceEnter("LoadUdpTable");
  210. if(g_Cache.pRpcUdpTable)
  211. {
  212. HeapFree(g_hPrivateHeap,
  213. 0,
  214. g_Cache.pRpcUdpTable);
  215. g_Cache.pRpcUdpTable = NULL;
  216. }
  217. dwResult = InternalGetUdpTable(&(g_Cache.pRpcUdpTable),
  218. g_hPrivateHeap,
  219. 0);
  220. if(dwResult isnot NO_ERROR)
  221. {
  222. TRACE1("GetUdpTable failed with error %x",
  223. dwResult);
  224. TraceLeave("LoadUdpTable");
  225. return dwResult;
  226. }
  227. //
  228. // modify port numbers to be in host byte order
  229. //
  230. for (i = 0; i < g_Cache.pRpcUdpTable->dwNumEntries; i++)
  231. {
  232. g_Cache.pRpcUdpTable->table[i].dwLocalPort =
  233. (DWORD)ntohs((WORD)g_Cache.pRpcUdpTable->table[i].dwLocalPort);
  234. }
  235. TraceLeave("LoadUdpTable");
  236. return NO_ERROR;
  237. }
  238. DWORD
  239. LoadUdp6ListenerTable()
  240. {
  241. DWORD dwResult, i;
  242. TraceEnter("LoadUdp6ListenerTable");
  243. if(g_Cache.pRpcUdp6ListenerTable)
  244. {
  245. HeapFree(g_hPrivateHeap,
  246. 0,
  247. g_Cache.pRpcUdp6ListenerTable);
  248. g_Cache.pRpcUdp6ListenerTable = NULL;
  249. }
  250. dwResult = AllocateAndGetUdpExTableFromStack(
  251. (UDP_EX_TABLE **)&(g_Cache.pRpcUdp6ListenerTable),
  252. TRUE, g_hPrivateHeap, 0, AF_INET6);
  253. if(dwResult isnot NO_ERROR)
  254. {
  255. TRACE1("GetUdp6ListenerTable failed with error %x",
  256. dwResult);
  257. TraceLeave("LoadUdp6ListenerTable");
  258. return dwResult;
  259. }
  260. //
  261. // modify port numbers to be in host byte order
  262. // and scope ids to be in network byte order
  263. //
  264. for (i = 0; i < g_Cache.pRpcUdp6ListenerTable->dwNumEntries; i++)
  265. {
  266. g_Cache.pRpcUdp6ListenerTable->table[i].ule_localport =
  267. (DWORD)ntohs((WORD)g_Cache.pRpcUdp6ListenerTable->table[i].ule_localport);
  268. g_Cache.pRpcUdp6ListenerTable->table[i].ule_localscopeid =
  269. htonl(g_Cache.pRpcUdp6ListenerTable->table[i].ule_localscopeid);
  270. }
  271. TraceLeave("LoadUdp6ListenerTable");
  272. return NO_ERROR;
  273. }
  274. DWORD
  275. GetSysInfo(
  276. MIB_SYSINFO **ppRpcSysInfo,
  277. HANDLE hHeap,
  278. DWORD dwAllocFlags
  279. )
  280. {
  281. DWORD dwResult,dwValueType,dwValueLen;
  282. PMIB_SYSINFO pRpcSysInfo;
  283. HKEY hkeySysInfo;
  284. DWORD dwBytes = 0, i, dwOidLen;
  285. PCHAR pchBuff, pchStr, pchToken;
  286. BOOL bOverride;
  287. TraceEnter("GetSysInfo");
  288. *ppRpcSysInfo = NULL;
  289. pRpcSysInfo = HeapAlloc(hHeap,
  290. dwAllocFlags,
  291. sizeof(MIB_SYSINFO));
  292. if(pRpcSysInfo is NULL)
  293. {
  294. dwResult = GetLastError();
  295. TRACE1("Allocation failed with error %d",
  296. dwResult);
  297. TraceLeave("GetSysInfo");
  298. return dwResult;
  299. }
  300. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  301. REG_KEY_MIB2,
  302. 0,
  303. KEY_ALL_ACCESS,
  304. &hkeySysInfo);
  305. if (dwResult isnot NO_ERROR) {
  306. HeapFree(hHeap,
  307. dwAllocFlags,
  308. pRpcSysInfo);
  309. TRACE1("Couldnt open mib2 registry key. Error %d", dwResult);
  310. TraceLeave("GetSysInfo");
  311. return dwResult;
  312. }
  313. dwValueLen = sizeof(pRpcSysInfo->dwSysServices);
  314. dwResult = RegQueryValueEx(hkeySysInfo,
  315. TEXT("sysServices"),
  316. 0,
  317. &dwValueType,
  318. (LPBYTE)&pRpcSysInfo->dwSysServices,
  319. &dwValueLen
  320. );
  321. if (dwResult isnot NO_ERROR) {
  322. HeapFree(hHeap,
  323. dwAllocFlags,
  324. pRpcSysInfo);
  325. TRACE1("Couldnt read sysServices value. Error %d", dwResult);
  326. TraceLeave("GetSysInfo");
  327. return dwResult;
  328. }
  329. bOverride = FALSE;
  330. do
  331. {
  332. //
  333. // First get the length of the OID
  334. //
  335. dwValueLen = 0;
  336. dwResult = RegQueryValueExA(hkeySysInfo,
  337. "sysObjectID",
  338. 0,
  339. &dwValueType,
  340. NULL,
  341. &dwValueLen);
  342. if(((dwResult isnot ERROR_MORE_DATA) and (dwResult isnot NO_ERROR)) or
  343. (dwValueLen is 0))
  344. {
  345. //
  346. // The only two codes that give us a good buffer len are
  347. // NO_ERROR and ERROR_MORE_DATA. If the error code is not one
  348. // of those, or if the oid len is 0, just set the OID to system oid
  349. //
  350. break;
  351. }
  352. pchBuff = HeapAlloc(g_hPrivateHeap,
  353. HEAP_ZERO_MEMORY,
  354. dwValueLen + 1);
  355. if(pchBuff is NULL)
  356. {
  357. break;
  358. }
  359. dwResult = RegQueryValueExA(hkeySysInfo,
  360. "sysObjectID",
  361. 0,
  362. &dwValueType,
  363. pchBuff,
  364. &dwValueLen);
  365. if((dwResult isnot NO_ERROR) or
  366. (dwValueType isnot REG_SZ) or
  367. (dwValueLen is 0))
  368. {
  369. break;
  370. }
  371. //
  372. // Parse out the oid and store it away
  373. // pchBuff is NULL terminated so we use strtok to overwrite
  374. // all the "." with \0. This way we figure out the number
  375. // of ids. Then we allocate memory to hold those many ids
  376. //
  377. dwOidLen = 1;
  378. pchToken = strtok(pchBuff,".");
  379. while(pchToken)
  380. {
  381. dwOidLen++;
  382. pchToken = strtok(NULL,".");
  383. }
  384. //
  385. // If the leading OID is 0, there is a problem
  386. //
  387. if(atoi(pchBuff) is 0)
  388. {
  389. break;
  390. }
  391. pRpcSysInfo->aaSysObjectID.asnType = ASN_OBJECTIDENTIFIER;
  392. pRpcSysInfo->aaSysObjectID.asnValue.object.idLength = dwOidLen;
  393. pRpcSysInfo->aaSysObjectID.asnValue.object.ids =
  394. SnmpUtilMemAlloc(dwOidLen * sizeof(UINT));
  395. for(i = 0, pchStr = pchBuff; i < dwOidLen; i++)
  396. {
  397. pRpcSysInfo->aaSysObjectID.asnValue.object.ids[i] = atoi(pchStr);
  398. pchStr += strlen(pchStr) + 1;
  399. }
  400. HeapFree(g_hPrivateHeap,
  401. 0,
  402. pchBuff);
  403. bOverride = TRUE;
  404. }while(FALSE);
  405. if(!bOverride)
  406. {
  407. SnmpUtilOidCpy(&pRpcSysInfo->aaSysObjectID.asnValue.object,
  408. SnmpSvcGetEnterpriseOID());
  409. }
  410. dwValueLen = sizeof(pRpcSysInfo->rgbySysName);
  411. dwResult = RegQueryValueEx(hkeySysInfo,
  412. TEXT("sysName"),
  413. 0,
  414. &dwValueType,
  415. pRpcSysInfo->rgbySysName,
  416. &dwValueLen
  417. );
  418. if (dwResult isnot NO_ERROR) {
  419. TRACE1("Couldnt read sysName value. Error %d", dwResult);
  420. dwValueLen = sizeof(pRpcSysInfo->rgbySysName);
  421. if (!GetComputerNameA(pRpcSysInfo->rgbySysName, &dwValueLen)) {
  422. HeapFree(hHeap,
  423. dwAllocFlags,
  424. pRpcSysInfo);
  425. dwResult = GetLastError();
  426. TRACE1("Couldnt read computer name. Error %d", dwResult);
  427. TraceLeave("GetSysInfo");
  428. return dwResult;
  429. }
  430. }
  431. dwValueLen = sizeof(pRpcSysInfo->rgbySysContact);
  432. dwResult = RegQueryValueEx(hkeySysInfo,
  433. TEXT("sysContact"),
  434. 0,
  435. &dwValueType,
  436. pRpcSysInfo->rgbySysContact,
  437. &dwValueLen
  438. );
  439. if (dwResult isnot NO_ERROR) {
  440. pRpcSysInfo->rgbySysContact[0] = '\0';
  441. }
  442. dwValueLen = sizeof(pRpcSysInfo->rgbySysLocation);
  443. dwResult = RegQueryValueEx(hkeySysInfo,
  444. TEXT("sysLocation"),
  445. 0,
  446. &dwValueType,
  447. pRpcSysInfo->rgbySysLocation,
  448. &dwValueLen
  449. );
  450. if (dwResult isnot NO_ERROR)
  451. {
  452. pRpcSysInfo->rgbySysLocation[0] = '\0';
  453. }
  454. RegCloseKey(hkeySysInfo);
  455. strcpy(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("Hardware: "));
  456. dwBytes += strlen(TEXT("Hardware: "));
  457. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  458. REG_KEY_CPU,
  459. 0,
  460. KEY_QUERY_VALUE |
  461. KEY_ENUMERATE_SUB_KEYS,
  462. &hkeySysInfo);
  463. if (dwResult is NO_ERROR) {
  464. dwValueLen = sizeof(pRpcSysInfo->rgbySysDescr) - dwBytes;
  465. dwResult = RegQueryValueEx(hkeySysInfo,
  466. TEXT("Identifier"),
  467. 0,
  468. &dwValueType,
  469. &pRpcSysInfo->rgbySysDescr[dwBytes],
  470. &dwValueLen);
  471. if (dwResult is NO_ERROR) {
  472. dwBytes += dwValueLen - 1;
  473. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes++], TEXT(" "));
  474. } else {
  475. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("CPU Unknown "));
  476. dwBytes += strlen(TEXT("CPU Unknown "));
  477. }
  478. RegCloseKey(hkeySysInfo);
  479. } else {
  480. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("CPU Unknown "));
  481. dwBytes += strlen(TEXT("CPU Unknown "));
  482. }
  483. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  484. REG_KEY_SYSTEM,
  485. 0,
  486. KEY_QUERY_VALUE |
  487. KEY_ENUMERATE_SUB_KEYS,
  488. &hkeySysInfo);
  489. if (dwResult is NO_ERROR) {
  490. dwValueLen = sizeof(pRpcSysInfo->rgbySysDescr) - dwBytes;
  491. dwResult = RegQueryValueEx(hkeySysInfo,
  492. TEXT("Identifier"),
  493. 0,
  494. &dwValueType,
  495. &pRpcSysInfo->rgbySysDescr[dwBytes],
  496. &dwValueLen);
  497. if (dwResult is NO_ERROR) {
  498. dwBytes += dwValueLen - 1;
  499. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes++], TEXT(" "));
  500. } else {
  501. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("SystemType Unknown "));
  502. dwBytes += strlen(TEXT("SystemType Unknown "));
  503. }
  504. RegCloseKey(hkeySysInfo);
  505. } else {
  506. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("SystemType Unknown "));
  507. dwBytes += strlen(TEXT("SystemType Unknown "));
  508. }
  509. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("- Software: Windows 2000 Version "));
  510. dwBytes += strlen(TEXT("- Software: Windows 2000 Version "));
  511. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  512. REG_KEY_VERSION,
  513. 0,
  514. KEY_QUERY_VALUE |
  515. KEY_ENUMERATE_SUB_KEYS,
  516. &hkeySysInfo);
  517. if (dwResult is NO_ERROR) {
  518. dwValueLen = sizeof(pRpcSysInfo->rgbySysDescr) - dwBytes;
  519. dwResult = RegQueryValueEx(hkeySysInfo,
  520. TEXT("CurrentVersion"),
  521. 0,
  522. &dwValueType,
  523. &pRpcSysInfo->rgbySysDescr[dwBytes],
  524. &dwValueLen);
  525. if (dwResult is NO_ERROR) {
  526. dwBytes += dwValueLen - 1;
  527. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes++], TEXT(" "));
  528. } else {
  529. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("Unknown "));
  530. dwBytes += strlen(TEXT("Unknown "));
  531. }
  532. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("(Build "));
  533. dwBytes += strlen(TEXT("(Build "));
  534. dwValueLen = sizeof(pRpcSysInfo->rgbySysDescr) - dwBytes;
  535. dwResult = RegQueryValueEx(hkeySysInfo,
  536. TEXT("CurrentBuildNumber"),
  537. 0,
  538. &dwValueType,
  539. &pRpcSysInfo->rgbySysDescr[dwBytes],
  540. &dwValueLen);
  541. if (dwResult is NO_ERROR) {
  542. dwBytes += dwValueLen - 1;
  543. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes++], TEXT(" "));
  544. } else {
  545. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("Unknown "));
  546. dwBytes += strlen(TEXT("Unknown "));
  547. }
  548. dwValueLen = sizeof(pRpcSysInfo->rgbySysDescr) - dwBytes;
  549. dwResult = RegQueryValueEx(hkeySysInfo,
  550. TEXT("CurrentType"),
  551. 0,
  552. &dwValueType,
  553. &pRpcSysInfo->rgbySysDescr[dwBytes],
  554. &dwValueLen);
  555. if (dwResult is NO_ERROR) {
  556. dwBytes += dwValueLen - 1;
  557. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes++], TEXT(")"));
  558. } else {
  559. pRpcSysInfo->rgbySysDescr[dwBytes - 1] = ')';
  560. }
  561. RegCloseKey(hkeySysInfo);
  562. } else {
  563. strcat(&pRpcSysInfo->rgbySysDescr[dwBytes], TEXT("Unknown"));
  564. dwBytes += strlen(TEXT("Unknown"));
  565. }
  566. *ppRpcSysInfo = pRpcSysInfo;
  567. TraceLeave("GetSysInfo");
  568. return NO_ERROR;
  569. }