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.

618 lines
17 KiB

  1. #include "stdafx.h"
  2. #include "WmiGateway.h"
  3. #include "util.h"
  4. #ifndef DIAGNOSTICS_H
  5. #define DIAGNOSTICS_H
  6. class CDglogsCom;
  7. // Field types. Describes the field
  8. //
  9. #define TYPE_PING 0x01
  10. #define TYPE_CONNECT 0x02
  11. #define TYPE_SUBNET 0x04
  12. #define TYPE_TEXT 0x08
  13. #define TYPE_HIDE 0x10
  14. #define TYPE_IP 0x20
  15. // Flags indicating what type of data should be displayed
  16. //
  17. #define FLAG_VERBOSE_LOW 0x01
  18. #define FLAG_VERBOSE_MEDIUM 0x02
  19. #define FLAG_VERBOSE_HIGH 0x04
  20. #define FLAG_CMD_PING 0x08
  21. #define FLAG_CMD_SHOW 0x10
  22. #define FLAG_CMD_CONNECT 0x20
  23. #define CMD_SHOW 0x01
  24. #define CMD_PING 0x02
  25. #define CMD_CONNECT 0x04
  26. class CDiagnostics
  27. {
  28. public:
  29. BOOL
  30. IsInvalidIPAddress(
  31. IN LPCSTR pszHostName
  32. );
  33. BOOL
  34. IsInvalidIPAddress(
  35. IN LPCWSTR pszHostName
  36. );
  37. inline BOOL ShouldTerminate();
  38. void SetCancelOption(HANDLE hTerminateThread)
  39. {
  40. m_hTerminateThread = hTerminateThread;
  41. m_bTerminate;
  42. }
  43. void CDiagnostics::ClearQuery();
  44. private:
  45. HANDLE m_hTerminateThread;
  46. BOOL m_bTerminate;
  47. wstring m_wstrCatagories;
  48. private:
  49. void
  50. CDiagnostics::FillIcmpData(
  51. IN OUT CHAR *pIcmp,
  52. IN DWORD dwDataSize
  53. );
  54. DWORD
  55. CDiagnostics::DecodeResponse(
  56. IN PCHAR pBuf,
  57. IN int nBytes,
  58. IN struct sockaddr_in *pFrom,
  59. IN int nIndent
  60. );
  61. USHORT
  62. CDiagnostics::CheckSum(
  63. IN USHORT *pBuffer,
  64. IN DWORD dwSize
  65. );
  66. int
  67. CDiagnostics::Ping(
  68. IN LPCTSTR pszwHostName,
  69. IN int nIndent
  70. );
  71. int
  72. CDiagnostics::Ping2(WCHAR *warg);
  73. void
  74. CDiagnostics::print_statistics( );
  75. BOOL
  76. CDiagnostics::Connect(
  77. IN LPCTSTR pszwHostName,
  78. IN DWORD dwPort
  79. );
  80. public:
  81. CDiagnostics();
  82. BOOLEAN m_bAdaterHasIPAddress;
  83. void SetMachine(WCHAR *pszMachine)
  84. {
  85. m_WmiGateway.SetMachine(pszMachine);
  86. }
  87. wstring &CDiagnostics::Escape(LPCTSTR pszw);
  88. void SetInterface(INTERFACE_TYPE bInterface);
  89. BOOLEAN Initialize(INTERFACE_TYPE bInterface);
  90. void SetQuery(WCHAR *pszwCatagory = NULL, BOOL bFlag = NULL, WCHAR *pszwParam1 = NULL, WCHAR *pszwParam2 = NULL);
  91. BOOLEAN ExecQuery(WCHAR *pszwCatagory = NULL, BOOL bFlag = NULL, WCHAR *pszwParam1 = NULL, WCHAR *pszwParam2 = NULL);
  92. BOOLEAN ExecClientQuery(WCHAR *pszwInstance);
  93. BOOLEAN ExecModemQuery(WCHAR *pszwInstance);
  94. BOOLEAN RemoveInvalidAdapters(EnumWbemProperty & PropList);
  95. BOOLEAN ExecAdapterQuery(WCHAR *pszwInstance);
  96. BOOLEAN ExecDNSQuery(WCHAR *pszwInstance);
  97. BOOLEAN ExecIPQuery(WCHAR *pszwInstance);
  98. BOOLEAN ExecWinsQuery(WCHAR *pszwInstance);
  99. BOOLEAN ExecGatewayQuery(WCHAR *pszwInstance);
  100. BOOLEAN ExecDhcpQuery(WCHAR *pszwInstance);
  101. BOOLEAN ExecComputerQuery();
  102. BOOLEAN ExecOSQuery();
  103. BOOLEAN ExecVersionQuery();
  104. BOOLEAN ExecProxyQuery();
  105. BOOLEAN ExecNewsQuery();
  106. BOOLEAN ExecMailQuery();
  107. BOOLEAN ExecLoopbackQuery();
  108. BOOLEAN CDiagnostics::ExecIPHost(WCHAR *pszwHostName,WCHAR *pszwHostPort);
  109. BOOLEAN IsLocked()
  110. {
  111. }
  112. WCHAR m_wszCaption[MAX_PATH*10];
  113. _bstr_t m_bstrCaption;
  114. template<class t>
  115. WCHAR * GetCaption(list<t> &l, DWORD nInstance)
  116. {
  117. list<t>::iterator iter;
  118. if( lstrcmp((WCHAR *)m_bstrCaption, L"") != 0 )
  119. {
  120. return (WCHAR *)m_bstrCaption;
  121. }
  122. for( iter = l.begin(); iter != l.end(); iter++)
  123. {
  124. if( lstrcmp(iter->pszwName,m_pszwCaption) == 0 )
  125. {
  126. if( iter->bFlags & TYPE_TEXT )
  127. {
  128. return m_pszwCaption;
  129. }
  130. if( nInstance < iter->Value.size() )
  131. {
  132. return iter->Value[nInstance].bstrVal;
  133. }
  134. return NULL;
  135. }
  136. }
  137. return NULL;
  138. }
  139. template<class t>
  140. DWORD GetNumberOfInstances(list<t> &l)
  141. {
  142. list<t>::iterator iter;
  143. if( l.empty() )
  144. {
  145. return 0;
  146. }
  147. iter = l.begin();
  148. return iter->Value.size();
  149. }
  150. template<class t>
  151. BOOLEAN FormatEnum(list<t> &l, WCHAR *pszwInstance = NULL, BOOLEAN bPass = 2)
  152. {
  153. WCHAR *pszwCaption = NULL;
  154. list<t>::iterator iter;
  155. WCHAR szwStatus[MAX_PATH];
  156. LONG nPercent;
  157. m_nIndex = 0;
  158. m_nInstance = GetNumberOfInstances(l);
  159. m_bCaptionDisplayed = FALSE;
  160. m_bHeaderStatus = 2;
  161. m_bValueStatus = 2;
  162. m_bCaptionStatus = bPass;
  163. if( ShouldTerminate() ) return FALSE;
  164. lstrcmp(szwStatus,L"");
  165. if( !m_WmiGateway.m_wstrWbemError.empty() )
  166. {
  167. XMLHeader(TRUE,m_szwHeader,ids(IDS_WMIERROR),m_szwCategory);
  168. NetShHeader(TRUE,m_szwHeader,ids(IDS_WMIERROR));
  169. XMLCaption(TRUE,NULL);
  170. NetShCaption(TRUE,NULL);
  171. XMLField(TRUE,NULL);
  172. NetShField(TRUE,NULL);
  173. if( (m_bFlags & FLAG_VERBOSE_LOW)==0 )
  174. {
  175. XMLProperty(TRUE,(WCHAR *)m_WmiGateway.m_wstrWbemError.c_str());
  176. NetShProperty(TRUE,(WCHAR *)m_WmiGateway.m_wstrWbemError.c_str());
  177. }
  178. XMLProperty(FALSE,NULL);
  179. NetShProperty(FALSE,NULL);
  180. XMLField(FALSE,NULL);
  181. NetShField(FALSE,NULL);
  182. XMLCaption(FALSE);
  183. NetShCaption(FALSE);
  184. XMLHeader(FALSE,ids(IDS_FAILED));
  185. NetShHeader(FALSE,ids(IDS_FAILED));
  186. return FALSE;
  187. }
  188. if( m_nInstance <= 1 )
  189. {
  190. pszwCaption = GetCaption(l,0);
  191. }
  192. XMLHeader(TRUE,m_szwHeader,pszwCaption,m_szwCategory);
  193. NetShHeader(TRUE,m_szwHeader,pszwCaption);
  194. if( m_nCatagoriesRequested && m_nInstance)
  195. {
  196. nPercent = 97/(m_nCatagoriesRequested * m_nInstance);
  197. }
  198. else
  199. {
  200. nPercent = 3;
  201. }
  202. m_bHeaderStatus = bPass; //2;
  203. for( DWORD i = 0; i < m_nInstance; i++)
  204. {
  205. m_bCaptionStatus = 2;
  206. if( ShouldTerminate() ) return FALSE;
  207. ReportStatus(m_szwHeader,nPercent);
  208. m_nIndex++;
  209. FormatInstance(l, i,pszwInstance);
  210. m_bCaptionDisplayed = FALSE;
  211. }
  212. if( m_bHeaderStatus == TRUE )
  213. {
  214. lstrcpy(szwStatus,ids(IDS_PASSED));
  215. }
  216. else
  217. if( m_bHeaderStatus == FALSE )
  218. {
  219. lstrcpy(szwStatus,ids(IDS_FAILED));
  220. }
  221. else
  222. {
  223. lstrcpy(szwStatus,L"");
  224. }
  225. XMLHeader(FALSE,szwStatus);
  226. NetShHeader(FALSE,szwStatus);
  227. return TRUE;
  228. }
  229. template<class t>
  230. BOOLEAN FormatInstance(list<t> &l, DWORD nInstance, WCHAR *pszwInstance)
  231. {
  232. list<t>::iterator iter;
  233. WCHAR *pszwCaption = GetCaption(l,nInstance);
  234. WCHAR szwStatus[MAX_PATH] = L"";
  235. m_bAdaterHasIPAddress = FALSE;
  236. for( iter = l.begin(); iter != l.end(); iter++)
  237. {
  238. if( lstrcmp(iter->pszwName,L"IPAddress") == 0 )
  239. {
  240. int nIndex = 0;
  241. _bstr_t bstr;
  242. while( SUCCEEDED(GetVariant(iter->Value[nInstance],nIndex,bstr)) && m_bAdaterHasIPAddress == FALSE)
  243. {
  244. if( lstrcmp((WCHAR *)bstr,L"0.0.0.0") != 0 && lstrcmp((WCHAR *)bstr,L"") != 0)
  245. {
  246. m_bAdaterHasIPAddress = TRUE;
  247. }
  248. nIndex++;
  249. }
  250. }
  251. }
  252. //m_bAdaterHasIPAddress = FALSE;
  253. m_bCaptionStatus = 2;
  254. if( pszwInstance )
  255. {
  256. if( IsNumber(pszwInstance) )
  257. {
  258. if( m_nIndex != wcstol(pszwInstance,NULL,10) )
  259. {
  260. return 2;
  261. }
  262. }
  263. else
  264. if( !IsContained(pszwCaption,pszwInstance) )
  265. {
  266. return 2;
  267. }
  268. }
  269. if( m_bFlags & FLAG_VERBOSE_LOW )
  270. {
  271. XMLCaption(TRUE,pszwCaption);
  272. NetShCaption(TRUE,pszwCaption);
  273. }
  274. else
  275. {
  276. for( iter = l.begin(); iter != l.end(); iter++)
  277. {
  278. m_bFieldStatus = 2;
  279. if( ShouldTerminate() ) return FALSE;
  280. if( iter->bFlags & TYPE_TEXT )
  281. {
  282. XMLCaption(TRUE,pszwCaption);
  283. XMLField(TRUE,NULL);
  284. XMLProperty(TRUE,iter->pszwName);
  285. XMLProperty(FALSE,NULL);
  286. XMLField(FALSE,NULL);
  287. NetShCaption(TRUE,pszwCaption);
  288. NetShField(TRUE,NULL);
  289. NetShProperty(TRUE,iter->pszwName);
  290. NetShProperty(FALSE,NULL);
  291. NetShField(FALSE,NULL);
  292. }
  293. else
  294. if( nInstance < iter->Value.size() && Filter(iter->Value[nInstance],iter->bFlags) )
  295. {
  296. XMLCaption(TRUE,pszwCaption);
  297. NetShCaption(TRUE,pszwCaption);
  298. XMLField(TRUE,iter->pszwName);
  299. NetShField(TRUE,iter->pszwName);
  300. FormatProperty(l,nInstance,iter->Value[nInstance], iter->bFlags);
  301. switch(m_bFieldStatus)
  302. {
  303. case TRUE:
  304. lstrcpy(szwStatus,ids(IDS_PASSED));
  305. break;
  306. case FALSE:
  307. lstrcpy(szwStatus,ids(IDS_FAILED));
  308. break;
  309. default:
  310. lstrcpy(szwStatus,L"");
  311. break;
  312. }
  313. XMLField(FALSE,szwStatus);
  314. NetShField(FALSE,szwStatus);
  315. }
  316. }
  317. }
  318. if( m_bCaptionStatus == TRUE )
  319. {
  320. lstrcpy(szwStatus,ids(IDS_PASSED));
  321. }
  322. else if( m_bCaptionStatus == FALSE )
  323. {
  324. lstrcpy(szwStatus,ids(IDS_FAILED));
  325. }
  326. else
  327. {
  328. lstrcpy(szwStatus,L"");
  329. }
  330. XMLCaption(FALSE,szwStatus);
  331. NetShCaption(FALSE,szwStatus);
  332. return 0;
  333. }
  334. template<class t>
  335. BOOLEAN FormatProperty(list<t> &l, DWORD nInstance, _variant_t &vValue, BOOLEAN bFlags)
  336. {
  337. _bstr_t bstrValue;
  338. LPCTSTR pszwData = NULL;
  339. LPCTSTR pszwComment = NULL;
  340. WCHAR szwStatus[MAX_PATH];
  341. WCHAR szwComment[MAX_PATH];
  342. DWORD nIndex = 0;
  343. INT nValue =0;
  344. //BOOLEAN bStatus = 2;
  345. while( SUCCEEDED(GetVariant(vValue,nIndex++,bstrValue)) )
  346. {
  347. nValue++;
  348. m_bValueStatus = 2;
  349. if( ShouldTerminate() ) return FALSE;
  350. pszwData = NULL;
  351. lstrcpy(szwStatus,L"");
  352. lstrcpy(szwComment,L"");
  353. if( lstrcmp((WCHAR *)bstrValue,L"") == 0)
  354. {
  355. bstrValue = ids(IDS_EMPTY);
  356. NetShProperty(TRUE,bstrValue,szwComment);
  357. }
  358. else
  359. {
  360. if( ((bFlags & TYPE_SUBNET) || (bFlags & TYPE_PING)) && IsInvalidIPAddress((WCHAR*)bstrValue))
  361. {
  362. // Should we report an error i.e. invalid IP address?
  363. //
  364. lstrcpy(szwComment,ids(IDS_INVALIDIP));
  365. NetShProperty(TRUE,bstrValue,szwComment);
  366. }
  367. else
  368. {
  369. if( (bFlags & TYPE_SUBNET) )
  370. {
  371. if( IsSameSubnet(Get(l,L"IPAddress",nInstance),Get(l,L"IPSubnet",nInstance),(WCHAR *)bstrValue) )
  372. {
  373. lstrcpy(szwComment,ids(IDS_SAMESUBNET));
  374. }
  375. else
  376. {
  377. lstrcpy(szwComment,ids(IDS_DIFFERENTSUBNET));
  378. }
  379. }
  380. NetShProperty(TRUE,bstrValue,szwComment);
  381. if( (bFlags & TYPE_PING) && (m_bFlags & FLAG_CMD_PING))
  382. {
  383. if( IsInvalidIPAddress((WCHAR*)bstrValue) )
  384. {
  385. lstrcpy(szwComment,ids(IDS_INVALIDIP));
  386. }
  387. else
  388. {
  389. WCHAR szw[MAX_PATH];
  390. wsprintf(szw,ids(IDS_PINGING_STATUS),(WCHAR *)bstrValue);
  391. ReportStatus(szw,0);
  392. if( Ping2(bstrValue) )
  393. {
  394. m_bHeaderStatus = m_bHeaderStatus == FALSE ? FALSE : TRUE;
  395. m_bCaptionStatus = m_bCaptionStatus == FALSE ? FALSE : TRUE;
  396. m_bFieldStatus = m_bFieldStatus == FALSE ? FALSE : TRUE;
  397. m_bValueStatus = TRUE;
  398. }
  399. else
  400. {
  401. m_bHeaderStatus = FALSE;
  402. m_bCaptionStatus = FALSE;
  403. m_bFieldStatus = FALSE;
  404. m_bValueStatus = FALSE;
  405. }
  406. pszwData = m_wstrPing.c_str();
  407. }
  408. }
  409. }
  410. }
  411. if( m_bValueStatus == TRUE )
  412. {
  413. lstrcpy(szwStatus,ids(IDS_PASSED));
  414. }
  415. else if( m_bValueStatus == FALSE )
  416. {
  417. lstrcpy(szwStatus,ids(IDS_FAILED));
  418. }
  419. else
  420. {
  421. lstrcpy(szwStatus,L"");
  422. }
  423. XMLProperty(TRUE,bstrValue,pszwData,szwComment);
  424. XMLProperty(FALSE,szwStatus);
  425. NetShProperty(FALSE,szwStatus);
  426. }
  427. if( nValue == 0 )
  428. {
  429. NetShProperty(TRUE,ids(IDS_EMPTY),L"");
  430. NetShProperty(FALSE,L"");
  431. }
  432. return 0; //bStatus;
  433. }
  434. template<class t>
  435. void HideAll(list<t> &l)
  436. {
  437. list<t>::iterator iter;
  438. for(iter = l.begin(); iter != l.end(); iter++)
  439. {
  440. iter->bFlags = TYPE_HIDE;
  441. }
  442. }
  443. BOOLEAN FormatPing(WCHAR * pszwText);
  444. BOOLEAN GetXMLResult(BSTR *pbstr);
  445. BOOLEAN Filter(_variant_t &vValue, BOOLEAN bFlags);
  446. void RequestStatusReport(BOOLEAN bReportStatus, CDglogsCom *pDglogsCom)
  447. {
  448. m_bReportStatus = bReportStatus;
  449. m_pDglogsCom = pDglogsCom;
  450. }
  451. void ReportStatus(LPCTSTR pszwMsg, LONG lValue);
  452. ~CDiagnostics();
  453. public:
  454. wstring m_wstrXML;
  455. wstring m_wstrText;
  456. wstring m_wstrPing;
  457. private:
  458. wstring m_wstrEscapeXml;
  459. INTERFACE_TYPE m_bInterface;
  460. CWmiGateway m_WmiGateway;
  461. WCHAR * m_pszwCaption;
  462. WCHAR m_szwHeader[MAX_PATH * 10];
  463. WCHAR m_szwCategory[MAX_PATH * 10];
  464. BOOLEAN m_bCaptionDisplayed;
  465. DWORD m_nInstance;
  466. BOOLEAN m_bStatus;
  467. private:
  468. PWCHAR m_pszwCatagory;
  469. BOOL m_bFlags;
  470. PWCHAR m_pszwParam1;
  471. PWCHAR m_pszwParam2;
  472. public:
  473. CDglogsCom *m_pDglogsCom;
  474. private:
  475. void EventCall(LPCTSTR pszwStatusReport, LONG lPercent);
  476. private:
  477. BOOLEAN m_IsNetdiagDisplayed;
  478. BOOLEAN m_IsContainerDisplayed;
  479. BOOLEAN m_IsPropertyListDisplayed;
  480. BOOLEAN m_IsPropertyDisplayed;
  481. BOOLEAN m_IsValueDisplayed;
  482. DWORD m_nPropertyLegth;
  483. DWORD m_nValueIndex;
  484. DWORD m_nIndent;
  485. DWORD m_nIndex;
  486. BOOLEAN m_bReportStatus;
  487. WCHAR m_szwStatusReport[MAX_PATH * 10];
  488. LONG m_lTotalWork;
  489. LONG m_lWorkDone;
  490. LONG m_nCatagoriesRequested;
  491. BOOLEAN m_bHeaderStatus;
  492. BOOLEAN m_bCaptionStatus;
  493. BOOLEAN m_bFieldStatus;
  494. BOOLEAN m_bPropertyStatus;
  495. BOOLEAN m_bValueStatus;
  496. private:
  497. void XMLNetdiag(BOOLEAN bStartTag, LPCTSTR pszwValue = NULL);
  498. void XMLHeader(BOOLEAN bStartTag, WCHAR *pszwHeader = NULL, WCHAR *pszwCaption = NULL, WCHAR *pszwCategory = NULL);
  499. void XMLCaption(BOOLEAN bStartTag, WCHAR *pszwCaption = NULL);
  500. void XMLField(BOOLEAN bStartTag, WCHAR *pszwField = NULL);
  501. void XMLProperty(BOOLEAN bStartTag, WCHAR *pszwProperty = NULL, LPCTSTR pszwData = NULL, LPCTSTR pszwComment = NULL);
  502. private:
  503. void NetShNetdiag(BOOLEAN bStartTag, LPCTSTR pszwValue = NULL);
  504. void NetShHeader(BOOLEAN bStartTag,LPCTSTR pszwValue = NULL,LPCTSTR pszwCaption = NULL);
  505. void NetShCaption(BOOLEAN bStartTag,LPCTSTR pszwValue = NULL);
  506. void NetShField(BOOLEAN bStartTag,LPCTSTR pszwValue = NULL);
  507. void NetShProperty(BOOLEAN bStartTag,LPCTSTR pszwValue = NULL,LPCTSTR pszwComment = NULL,BOOL bFlags = 0);
  508. };
  509. #endif