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.

477 lines
9.5 KiB

  1. /******************************************************************
  2. pingget.CPP
  3. Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  4. Description:
  5. ******************************************************************/
  6. #include <stdafx.h>
  7. #include <ntddtcp.h>
  8. #include <ipinfo.h>
  9. #include <tdiinfo.h>
  10. #include <winsock2.h>
  11. #include <provimex.h>
  12. #include <provexpt.h>
  13. #include <provtempl.h>
  14. #include <provmt.h>
  15. #include <typeinfo.h>
  16. #include <provcont.h>
  17. #include <provevt.h>
  18. #include <provthrd.h>
  19. #include <provlog.h>
  20. #include <provval.h>
  21. #include <provtype.h>
  22. #include <provtree.h>
  23. #include <provdnf.h>
  24. #include <winsock.h>
  25. #include "ipexport.h"
  26. #include "icmpapi.h"
  27. #include ".\res_str.h"
  28. #include <Allocator.h>
  29. #include <Thread.h>
  30. #include <HashTable.h>
  31. #include <PingProv.h>
  32. #include <Pingtask.h>
  33. #include <Pingfac.h>
  34. CPingGetAsync::CPingGetAsync (
  35. CPingProvider *a_Provider ,
  36. wchar_t *a_ObjectPath ,
  37. ULONG a_Flag ,
  38. IWbemObjectSink *a_NotificationHandler ,
  39. IWbemContext *a_Ctx
  40. ): m_ObjectPath(NULL),
  41. m_ParsedObjectPath (NULL),
  42. CPingTaskObject (a_Provider, a_NotificationHandler, a_Ctx)
  43. {
  44. if (a_ObjectPath != NULL)
  45. {
  46. int t_len = wcslen(a_ObjectPath);
  47. if (t_len > 0)
  48. {
  49. m_ObjectPath = new WCHAR[t_len+1];
  50. m_ObjectPath[t_len] = L'\0';
  51. wcsncpy(m_ObjectPath, a_ObjectPath, t_len);
  52. }
  53. }
  54. }
  55. CPingGetAsync::~CPingGetAsync ()
  56. {
  57. if (m_ObjectPath)
  58. {
  59. delete [] m_ObjectPath ;
  60. }
  61. if (m_ParsedObjectPath)
  62. {
  63. delete m_ParsedObjectPath ;
  64. }
  65. }
  66. BOOL CPingGetAsync::GetObject ()
  67. {
  68. InterlockedIncrement(&m_PingCount);
  69. BOOL t_Status = ! m_ObjectPathParser.Parse ( m_ObjectPath , &m_ParsedObjectPath ) ;
  70. if ( t_Status )
  71. {
  72. BOOL bClass = TRUE;
  73. if ( _wcsicmp ( m_ParsedObjectPath->m_pClass , PROVIDER_NAME_CPINGPROVIDER) == 0 )
  74. {
  75. IWbemClassObject *t_Cls = NULL;
  76. t_Status = GetClassObject ( &t_Cls ) ;
  77. if ( t_Status )
  78. {
  79. t_Status = PerformGet() ;
  80. t_Cls->Release();
  81. }
  82. else
  83. {
  84. t_Status = FALSE ;
  85. SetErrorInfo(IDS_CLASS_DEFN,
  86. WBEM_E_FAILED ) ;
  87. }
  88. }
  89. else
  90. {
  91. t_Status = FALSE ;
  92. SetErrorInfo(IDS_INVALID_CLASS ,
  93. WBEM_E_FAILED ) ;
  94. }
  95. }
  96. else
  97. {
  98. t_Status = FALSE ;
  99. SetErrorInfo(IDS_OBJ_PATH ,
  100. WBEM_E_FAILED ) ;
  101. }
  102. DecrementPingCount();
  103. return t_Status ;
  104. }
  105. BOOL CPingGetAsync::PerformGet ()
  106. {
  107. if (m_ParsedObjectPath->m_dwNumKeys != PING_KEY_PROPERTY_COUNT)
  108. {
  109. SetErrorInfo(IDS_OBJ_PATH_KEYS ,
  110. WBEM_E_FAILED ) ;
  111. return FALSE;
  112. }
  113. BOOL t_Status = TRUE;
  114. UCHAR t_bits[PING_KEY_PROPERTY_COUNT] = {0};
  115. ULONG t_Address = 0 ;
  116. ULONG t_TimeToLive = 0 ;
  117. ULONG t_Timeout = 0 ;
  118. ULONG t_SendSize = 0 ;
  119. BOOL t_NoFragmentation = FALSE ;
  120. ULONG t_TypeofService = 0 ;
  121. ULONG t_RecordRoute = 0 ;
  122. ULONG t_TimestampRoute = 0 ;
  123. ULONG t_SourceRouteType = 0 ;
  124. LPCWSTR t_SourceRoute = NULL ;
  125. BOOL t_ResolveAddress = FALSE;
  126. ULONG t_AddressKey = 0;
  127. ULONG t_ResolveErr = 0;
  128. for (int x = 0; ((x < PING_KEY_PROPERTY_COUNT) && t_Status); x++)
  129. {
  130. if (m_ParsedObjectPath->m_paKeys [ x ])
  131. {
  132. CKeyEntry t_key(m_ParsedObjectPath->m_paKeys [ x ]->m_pName) ;
  133. ULONG t_PropertyIndex = 0xFF; //out of scope
  134. if ( (CPingProvider::s_HashTable->Find(t_key, t_PropertyIndex) == e_StatusCode_Success)
  135. && (t_PropertyIndex < PING_KEY_PROPERTY_COUNT) )
  136. {
  137. if (t_bits[t_PropertyIndex] == 1)
  138. {
  139. t_Status = FALSE;
  140. SetErrorInfo(IDS_OBJ_PATH_DUP_KEYS ,
  141. WBEM_E_FAILED ) ;
  142. }
  143. else
  144. {
  145. t_bits[t_PropertyIndex] = 1;
  146. switch (t_PropertyIndex)
  147. {
  148. case PING_ADDRESS_INDEX:
  149. {
  150. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_BSTR )
  151. {
  152. if (FAILED (Icmp_ResolveAddress ( m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.bstrVal , t_Address, &t_ResolveErr ))
  153. && (t_ResolveErr == 0) )
  154. {
  155. t_ResolveErr = WSAHOST_NOT_FOUND;
  156. }
  157. t_AddressKey = x;
  158. }
  159. else
  160. {
  161. t_Status = FALSE;
  162. SetErrorInfo(IDS_ADDR_TYPE,
  163. WBEM_E_FAILED ) ;
  164. }
  165. }
  166. break;
  167. case PING_TIMEOUT_INDEX:
  168. {
  169. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  170. {
  171. t_Timeout = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  172. }
  173. else
  174. {
  175. t_Status = FALSE;
  176. SetErrorInfo(IDS_TO_TYPE,
  177. WBEM_E_FAILED ) ;
  178. }
  179. }
  180. break;
  181. case PING_TIMETOLIVE_INDEX:
  182. {
  183. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  184. {
  185. t_TimeToLive = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  186. }
  187. else
  188. {
  189. t_Status = FALSE;
  190. SetErrorInfo(IDS_TTL_TYPE,
  191. WBEM_E_FAILED ) ;
  192. }
  193. }
  194. break;
  195. case PING_BUFFERSIZE_INDEX:
  196. {
  197. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  198. {
  199. t_SendSize = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  200. }
  201. else
  202. {
  203. t_Status = FALSE;
  204. SetErrorInfo(IDS_BUFF_TYPE,
  205. WBEM_E_FAILED ) ;
  206. }
  207. }
  208. break;
  209. case PING_NOFRAGMENTATION_INDEX:
  210. {
  211. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  212. {
  213. t_NoFragmentation = (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal != 0);
  214. }
  215. else
  216. {
  217. t_Status = FALSE;
  218. SetErrorInfo(IDS_NOFRAG_TYPE,
  219. WBEM_E_FAILED ) ;
  220. }
  221. }
  222. break;
  223. case PING_TYPEOFSERVICE_INDEX:
  224. {
  225. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  226. {
  227. t_TypeofService = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  228. }
  229. else
  230. {
  231. t_Status = FALSE;
  232. SetErrorInfo(IDS_TOS_TYPE,
  233. WBEM_E_FAILED ) ;
  234. }
  235. }
  236. break;
  237. case PING_RECORDROUTE_INDEX:
  238. {
  239. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  240. {
  241. t_RecordRoute = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  242. }
  243. else
  244. {
  245. t_Status = FALSE;
  246. SetErrorInfo(IDS_RR_TYPE,
  247. WBEM_E_FAILED ) ;
  248. }
  249. }
  250. break;
  251. case PING_TIMESTAMPROUTE_INDEX:
  252. {
  253. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  254. {
  255. t_TimestampRoute = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  256. }
  257. else
  258. {
  259. t_Status = FALSE;
  260. SetErrorInfo(IDS_TS_TYPE,
  261. WBEM_E_FAILED ) ;
  262. }
  263. }
  264. break;
  265. case PING_SOURCEROUTETYPE_INDEX:
  266. {
  267. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  268. {
  269. t_SourceRouteType = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal;
  270. }
  271. else
  272. {
  273. t_Status = FALSE;
  274. SetErrorInfo(IDS_SRT_TYPE,
  275. WBEM_E_FAILED ) ;
  276. }
  277. }
  278. break;
  279. case PING_SOURCEROUTE_INDEX:
  280. {
  281. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_BSTR )
  282. {
  283. t_SourceRoute = m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.bstrVal;
  284. }
  285. else
  286. {
  287. t_Status = FALSE;
  288. SetErrorInfo(IDS_SR_TYPE,
  289. WBEM_E_FAILED ) ;
  290. }
  291. }
  292. break;
  293. case PING_RESOLVEADDRESSNAMES_INDEX:
  294. {
  295. if (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.vt == VT_I4 )
  296. {
  297. t_ResolveAddress = (m_ParsedObjectPath->m_paKeys [ x ]->m_vValue.lVal != 0);
  298. }
  299. else
  300. {
  301. t_Status = FALSE;
  302. SetErrorInfo(IDS_RA_TYPE,
  303. WBEM_E_FAILED ) ;
  304. }
  305. }
  306. break;
  307. default :
  308. {
  309. t_Status = FALSE;
  310. SetErrorInfo(IDS_UNK_PROP,
  311. WBEM_E_FAILED ) ;
  312. }
  313. }
  314. }
  315. }
  316. else
  317. {
  318. t_Status = FALSE;
  319. SetErrorInfo(IDS_UNK_KEY,
  320. WBEM_E_FAILED ) ;
  321. }
  322. }
  323. else
  324. {
  325. t_Status = FALSE;
  326. SetErrorInfo(IDS_NO_KEYS,
  327. WBEM_E_FAILED ) ;
  328. }
  329. }
  330. if (t_Status)
  331. {
  332. InterlockedIncrement(&m_PingCount);
  333. HRESULT t_Result = Icmp_RequestResponse (
  334. m_ParsedObjectPath->m_paKeys [ t_AddressKey ]->m_vValue.bstrVal ,
  335. t_Address ,
  336. t_TimeToLive ,
  337. t_Timeout ,
  338. t_SendSize ,
  339. t_NoFragmentation ,
  340. t_TypeofService ,
  341. t_RecordRoute ,
  342. t_TimestampRoute ,
  343. t_SourceRouteType ,
  344. t_SourceRoute,
  345. t_ResolveAddress,
  346. t_ResolveErr
  347. ) ;
  348. if ( SUCCEEDED ( t_Result ) )
  349. {
  350. SetErrorInfo(0, WBEM_E_NOT_FOUND ) ;
  351. }
  352. else
  353. {
  354. DecrementPingCount();
  355. t_Status = FALSE;
  356. }
  357. }
  358. return t_Status ;
  359. }
  360. HRESULT CPingGetAsync::GetDefaultTTL ( DWORD &a_TimeToLive )
  361. {
  362. HRESULT t_Result = S_OK ;
  363. a_TimeToLive = DEFAULT_TTL ;
  364. HKEY t_Registry ;
  365. ULONG t_Status = RegOpenKeyEx (
  366. HKEY_LOCAL_MACHINE,
  367. L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
  368. 0,
  369. KEY_QUERY_VALUE,
  370. &t_Registry
  371. ) ;
  372. if ( t_Status == ERROR_SUCCESS )
  373. {
  374. ULONG t_Type = 0 ;
  375. ULONG t_Size = sizeof ( ULONG ) ;
  376. t_Status = RegQueryValueEx (
  377. t_Registry,
  378. L"DefaultTTL",
  379. 0,
  380. & t_Type,
  381. ( unsigned char * ) & a_TimeToLive ,
  382. & t_Size
  383. );
  384. RegCloseKey ( t_Registry ) ;
  385. }
  386. return t_Result ;
  387. }
  388. void CPingGetAsync::HandleResponse (CPingCallBackObject *a_reply)
  389. {
  390. try
  391. {
  392. if (FAILED(Icmp_DecodeAndIndicate (a_reply)) )
  393. {
  394. SetErrorInfo(IDS_DECODE_GET,
  395. WBEM_E_FAILED, TRUE ) ;
  396. }
  397. else
  398. {
  399. SetErrorInfo(NULL, S_OK, TRUE);
  400. }
  401. }
  402. catch (...)
  403. {
  404. DecrementPingCount();
  405. }
  406. DecrementPingCount();
  407. }
  408. void CPingGetAsync::HandleErrorResponse (DWORD a_ErrMsgID, HRESULT a_HRes)
  409. {
  410. try
  411. {
  412. SetErrorInfo(a_ErrMsgID , a_HRes, TRUE) ;
  413. }
  414. catch (...)
  415. {
  416. DecrementPingCount();
  417. }
  418. DecrementPingCount();
  419. }