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.

756 lines
18 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: rascommain.cpp
  4. //
  5. // Synopsis: this is the main Source File for the RAS Server
  6. // COM component DLL
  7. //
  8. //
  9. // History: 2/10/98 MKarki Created
  10. // 8/04/98 MKarki Changes for Dynamic Config
  11. //
  12. // Copyright (C) 1997-98 Microsoft Corporation
  13. // All rights reserved.
  14. //
  15. //----------------------------------------------------------------
  16. //
  17. // Note: Proxy/Stub Information
  18. // To build a separate proxy/stub DLL,
  19. // run nmake -f radprotops.mk in the project directory.
  20. #include "rascominclude.h"
  21. #include "crascom.h"
  22. CComModule _Module;
  23. BEGIN_OBJECT_MAP(ObjectMap)
  24. OBJECT_ENTRY(__uuidof(IasHelper), CRasCom)
  25. END_OBJECT_MAP()
  26. //
  27. // globals
  28. //
  29. IRecvRequest *g_pIRecvRequest = NULL;
  30. ISdoService *g_pISdoService = NULL;
  31. BOOL g_bInitialized = FALSE;
  32. CRITICAL_SECTION g_SrvCritSect;
  33. //
  34. // ProgID of SdoService component
  35. //
  36. const WCHAR SERVICE_PROG_ID[] = L"IAS.SdoService";
  37. //
  38. // ProgID for IasHelper component
  39. //
  40. const WCHAR HELPER_PROG_ID[] = L"IAS.IasHelper";
  41. //++--------------------------------------------------------------
  42. //
  43. // Function: DllMain
  44. //
  45. // Synopsis: Disabling thread calls
  46. //
  47. // Arguments: [in] HINSTANCE - module handle
  48. // [in] DWORD - reason for call
  49. // reserved
  50. //
  51. // Returns: BOOL - sucess/failure
  52. //
  53. // History: MKarki Created 2/10/98
  54. //
  55. //----------------------------------------------------------------
  56. extern "C" BOOL WINAPI
  57. DllMain(
  58. HINSTANCE hInstance,
  59. DWORD dwReason,
  60. LPVOID lpReserved
  61. )
  62. {
  63. if (dwReason == DLL_PROCESS_ATTACH)
  64. {
  65. InitializeCriticalSection (&g_SrvCritSect);
  66. _Module.Init(ObjectMap, hInstance);
  67. DisableThreadLibraryCalls(hInstance);
  68. }
  69. else if (dwReason == DLL_PROCESS_DETACH)
  70. {
  71. DeleteCriticalSection (&g_SrvCritSect);
  72. _Module.Term();
  73. }
  74. return (TRUE);
  75. } // end of DllMain method
  76. //++--------------------------------------------------------------
  77. //
  78. // Function: DllCanUnloadNow
  79. //
  80. // Synopsis: Used to determine if the DLL can be unloaded
  81. //
  82. // Arguments: NONE
  83. //
  84. // Returns: HRESULT
  85. //
  86. //
  87. // History: MKarki Created 8/20/97
  88. //
  89. //----------------------------------------------------------------
  90. STDAPI
  91. DllCanUnloadNow(
  92. VOID
  93. )
  94. {
  95. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  96. } // end of DllCanUnloadNow method
  97. //++--------------------------------------------------------------
  98. //
  99. // Function: DllGetClassObject
  100. //
  101. // Synopsis: Returns a class factory to create an object
  102. // of the requested type
  103. //
  104. // Arguments: [in] REFCLSID
  105. // [in] REFIID
  106. // [out] LPVOID - class factory
  107. //
  108. // Returns: HRESULT
  109. //
  110. // History: MKarki Created 2/10/98
  111. //
  112. //----------------------------------------------------------------
  113. STDAPI
  114. DllGetClassObject(
  115. REFCLSID rclsid,
  116. REFIID riid,
  117. LPVOID* ppv
  118. )
  119. {
  120. return (_Module.GetClassObject(rclsid, riid, ppv));
  121. } // end of DllGetClassObject method
  122. //++--------------------------------------------------------------
  123. //
  124. // Function: DllRegisterServer
  125. //
  126. // Synopsis: Add entries to the system registry
  127. //
  128. // Arguments: NONE
  129. //
  130. // Returns: HRESULT
  131. //
  132. // History: MKarki Created 2/10/98
  133. //
  134. //----------------------------------------------------------------
  135. STDAPI DllRegisterServer(
  136. VOID
  137. )
  138. {
  139. //
  140. // registers object, typelib and all interfaces in typelib
  141. //
  142. return (_Module.RegisterServer(TRUE));
  143. } // end of DllRegisterServer method
  144. //++--------------------------------------------------------------
  145. //
  146. // Function: DllUnregisterServer
  147. //
  148. // Synopsis: Removes entries from the system registry
  149. //
  150. // Arguments: NONE
  151. //
  152. // Returns: HRESULT
  153. //
  154. // History: MKarki Created 2/10/98
  155. //
  156. //----------------------------------------------------------------
  157. STDAPI
  158. DllUnregisterServer(
  159. VOID
  160. )
  161. {
  162. _Module.UnregisterServer();
  163. return (S_OK);
  164. } // end of DllUnregisterServer method
  165. //++--------------------------------------------------------------
  166. //
  167. // Function: AllocateAttributes
  168. //
  169. // Synopsis: This API allocates the number of attributes spefied
  170. // and returns them in the PIASATTRIBUTE array
  171. //
  172. // Arguments:
  173. // [in] DWORD - number of attributes
  174. // [out] PIASATTRIBUTE* array
  175. //
  176. // Returns: HRESULT
  177. //
  178. // History: MKarki Created 2/10/98
  179. //
  180. //----------------------------------------------------------------
  181. STDAPI
  182. AllocateAttributes (
  183. DWORD dwAttributeCount,
  184. PIASATTRIBUTE *ppIasAttribute
  185. )
  186. {
  187. DWORD dwRetVal = 0;
  188. //
  189. // can only allocate attributes after initialization
  190. //
  191. if (FALSE == g_bInitialized)
  192. {
  193. IASTracePrintf (
  194. "InitializeIas method needs to has not been called before"
  195. "allocating attributes"
  196. );
  197. return (E_FAIL);
  198. }
  199. //
  200. // check if arguments are correct
  201. //
  202. if ((0 == dwAttributeCount) || (NULL == ppIasAttribute))
  203. {
  204. IASTracePrintf (
  205. "Inivalid arguments passed in to AllocateAttributes method"
  206. );
  207. return (E_INVALIDARG);
  208. }
  209. //
  210. // allocate attributes now
  211. //
  212. dwRetVal = ::IASAttributeAlloc (dwAttributeCount, ppIasAttribute);
  213. if (0 != dwRetVal)
  214. {
  215. IASTracePrintf (
  216. "Unable to allocate memory in AllocateAttributes method"
  217. );
  218. return (E_FAIL);
  219. }
  220. return (S_OK);
  221. } // end of AllocateAttributes method
  222. //++--------------------------------------------------------------
  223. //
  224. // Function: FreeAttributes
  225. //
  226. // Synopsis: This API frees the number of attributes spefied
  227. //
  228. // Arguments:
  229. // [in] DWORD - number of attributes
  230. // [in] PIASATTRIBUTE array
  231. //
  232. // Returns: HRESULT
  233. //
  234. // History: MKarki Created 2/10/98
  235. //
  236. //----------------------------------------------------------------
  237. STDAPI
  238. FreeAttributes (
  239. DWORD dwAttributeCount,
  240. PIASATTRIBUTE *ppIasAttribute
  241. )
  242. {
  243. DWORD dwCount = 0;
  244. //
  245. // can only free attributes after initialization
  246. //
  247. if (FALSE == g_bInitialized)
  248. {
  249. IASTracePrintf (
  250. "InitializeIas needs to be called before freeing attributes"
  251. );
  252. return (E_FAIL);
  253. }
  254. //
  255. // check if correct attributes have been passed in
  256. //
  257. if (NULL == ppIasAttribute)
  258. {
  259. IASTracePrintf (
  260. "Invalid arguments passed in to FreeAttributes method"
  261. );
  262. return (E_INVALIDARG);
  263. }
  264. //
  265. // free the attributes now
  266. //
  267. for (dwCount = 0; dwCount < dwAttributeCount; dwCount++)
  268. {
  269. ::IASAttributeRelease (ppIasAttribute[dwCount]);
  270. }
  271. return (S_OK);
  272. } // end of FreeAttributes method
  273. //++--------------------------------------------------------------
  274. //
  275. // Function: DoRequest
  276. //
  277. // Synopsis: This is the API that is called to send request
  278. // to the pipeline
  279. //
  280. // Arguments:
  281. // [in] DWORD - number of attributes
  282. // [in] PIASATTRIBUTE*
  283. // [in] IASREQUEST
  284. // [out] IASRESPONSE
  285. //
  286. // Returns: HRESULT - status
  287. //
  288. // History: MKarki Created 2/10/98
  289. //
  290. //----------------------------------------------------------------
  291. STDAPI
  292. DoRequest (
  293. DWORD dwInAttributeCount,
  294. PIASATTRIBUTE *ppInIasAttribute,
  295. PDWORD pdwOutAttributeCount,
  296. PIASATTRIBUTE **pppOutIasAttribute,
  297. LONG IasRequest,
  298. LONG *pIasResponse,
  299. IASPROTOCOL IasProtocol,
  300. PLONG plReason,
  301. BOOL bProcessVSA
  302. )
  303. {
  304. DWORD dwRetVal = 0;
  305. BOOL bStatus = FALSE;
  306. HRESULT hr = S_OK;
  307. //
  308. // can only make request to pipeline after initialization
  309. //
  310. if (FALSE == g_bInitialized)
  311. {
  312. IASTracePrintf (
  313. "InitializeIas needs to be called before Request processing"
  314. );
  315. return (E_FAIL);
  316. }
  317. //
  318. // check the arguments passed in
  319. //
  320. if (
  321. (NULL == ppInIasAttribute) ||
  322. (NULL == pdwOutAttributeCount) ||
  323. (NULL == pppOutIasAttribute) ||
  324. (NULL == pIasResponse) ||
  325. (NULL == plReason)
  326. )
  327. {
  328. IASTracePrintf (
  329. "Invalid arguments passed in to DoRequest method"
  330. );
  331. return (E_INVALIDARG);
  332. }
  333. //
  334. // make the request to the IASHelper COM Object interface
  335. //
  336. hr = g_pIRecvRequest->Process (
  337. dwInAttributeCount,
  338. ppInIasAttribute,
  339. pdwOutAttributeCount,
  340. pppOutIasAttribute,
  341. IasRequest,
  342. pIasResponse,
  343. IasProtocol,
  344. plReason,
  345. bProcessVSA
  346. );
  347. if (FAILED (hr))
  348. {
  349. IASTracePrintf ( "Surrogate failed in processing request...");
  350. }
  351. return (hr);
  352. } // end of DoRequest method
  353. //++--------------------------------------------------------------
  354. //
  355. // Function: InitializeIas
  356. //
  357. // Synopsis: This is the API that is called to Initialize the
  358. // IasHlpr Component
  359. //
  360. // Arguments: none
  361. //
  362. // Returns: HRESULT - status
  363. //
  364. // History: MKarki Created 2/10/98
  365. //
  366. //----------------------------------------------------------------
  367. extern "C" HRESULT WINAPI
  368. InitializeIas (
  369. BOOL bComInit
  370. )
  371. {
  372. HRESULT hr = S_OK;
  373. CLSID clsid;
  374. __try
  375. {
  376. EnterCriticalSection (&g_SrvCritSect);
  377. IASTracePrintf ("Initializing Surrogate...");
  378. //
  379. // check if we are already initialized
  380. //
  381. if (TRUE == g_bInitialized)
  382. {
  383. __leave;
  384. }
  385. //
  386. // check if our threads are COM enabled
  387. //
  388. if (FALSE == bComInit)
  389. {
  390. IASTracePrintf (
  391. "Thread calling InitializeIas need to be COM enabled"
  392. );
  393. hr = E_INVALIDARG;
  394. __leave;
  395. }
  396. //
  397. // convert SdoService ProgID to CLSID
  398. //
  399. hr = CLSIDFromProgID (SERVICE_PROG_ID, &clsid);
  400. if (FAILED (hr))
  401. {
  402. IASTracePrintf ( "Unable to get SDO service ID" );
  403. __leave;
  404. }
  405. //
  406. // Create the SdoComponent
  407. //
  408. hr = CoCreateInstance (
  409. clsid,
  410. NULL,
  411. CLSCTX_INPROC_SERVER,
  412. __uuidof (ISdoService),
  413. reinterpret_cast <PVOID*> (&g_pISdoService)
  414. );
  415. if (FAILED (hr))
  416. {
  417. IASTracePrintf ( "Unable to create Surrogate COM component");
  418. __leave;
  419. }
  420. //
  421. // Initialize the Service first
  422. //
  423. hr = g_pISdoService->InitializeService (SERVICE_TYPE_RAS);
  424. if (FAILED (hr))
  425. {
  426. IASTracePrintf ( "Unable to initialize SDO COM component");
  427. __leave;
  428. }
  429. //
  430. // Start the IAS service now
  431. //
  432. hr = g_pISdoService->StartService (SERVICE_TYPE_RAS);
  433. if (FAILED (hr))
  434. {
  435. IASTracePrintf ( "Unable to start SDO component");
  436. //
  437. // got to do a shutdown if could not start the sevice
  438. //
  439. HRESULT hr1 = g_pISdoService->ShutdownService (SERVICE_TYPE_RAS);
  440. if (FAILED (hr1))
  441. {
  442. IASTracePrintf("Unable to shutdown SDO compnent");
  443. }
  444. __leave;
  445. }
  446. //
  447. // convert IasHelper ProgID to CLSID
  448. //
  449. hr = CLSIDFromProgID (HELPER_PROG_ID, &clsid);
  450. if (FAILED (hr))
  451. {
  452. IASTracePrintf("Unable to obtain Surrogate ID");
  453. __leave;
  454. }
  455. //
  456. // Create the IasHelper component
  457. //
  458. hr = CoCreateInstance (
  459. clsid,
  460. NULL,
  461. CLSCTX_INPROC_SERVER,
  462. __uuidof (IRecvRequest),
  463. reinterpret_cast <PVOID*> (&g_pIRecvRequest)
  464. );
  465. if (FAILED (hr))
  466. {
  467. IASTracePrintf("Unable to create Surrogate component");
  468. __leave;
  469. }
  470. //
  471. // initialization complete
  472. //
  473. g_bInitialized = TRUE;
  474. }
  475. __finally
  476. {
  477. if (FAILED (hr))
  478. {
  479. IASTracePrintf ("Surrogate failed initialization.");
  480. //
  481. // do cleanup
  482. //
  483. if (NULL != g_pIRecvRequest)
  484. {
  485. g_pIRecvRequest->Release ();
  486. g_pIRecvRequest = NULL;
  487. }
  488. if (NULL != g_pISdoService)
  489. {
  490. g_pISdoService->Release ();
  491. g_pISdoService = NULL;
  492. }
  493. }
  494. else
  495. {
  496. IASTracePrintf ("Surrogate initialized.");
  497. }
  498. LeaveCriticalSection (&g_SrvCritSect);
  499. }
  500. return (hr);
  501. } // end of InitializeIas method
  502. //++--------------------------------------------------------------
  503. //
  504. // Function: ShutdownIas
  505. //
  506. // Synopsis: This is the API used to shutdown the
  507. // IasHlpr Component
  508. //
  509. // Arguments: none
  510. //
  511. // Returns: VOID
  512. //
  513. // History: MKarki Created 2/10/98
  514. //
  515. //----------------------------------------------------------------
  516. extern "C" VOID WINAPI
  517. ShutdownIas (
  518. VOID
  519. )
  520. {
  521. HRESULT hr = S_OK;
  522. EnterCriticalSection (&g_SrvCritSect);
  523. IASTracePrintf ("Shutting down Surrogate....");
  524. //
  525. // stop the components first
  526. //
  527. if (FALSE == g_bInitialized)
  528. {
  529. LeaveCriticalSection (&g_SrvCritSect);
  530. return;
  531. }
  532. else
  533. {
  534. //
  535. // don't let any requests come through now
  536. //
  537. g_bInitialized = FALSE;
  538. }
  539. //
  540. // release the reference to the interface
  541. //
  542. if (NULL != g_pIRecvRequest)
  543. {
  544. g_pIRecvRequest->Release ();
  545. g_pIRecvRequest = NULL;
  546. }
  547. if (NULL != g_pISdoService)
  548. {
  549. //
  550. // stop the service
  551. //
  552. hr = g_pISdoService->StopService (SERVICE_TYPE_RAS);
  553. if (FAILED (hr))
  554. {
  555. IASTracePrintf ("Unable to stop SDO component");
  556. }
  557. //
  558. // shutdown the service
  559. //
  560. hr = g_pISdoService->ShutdownService (SERVICE_TYPE_RAS);
  561. if (FAILED (hr))
  562. {
  563. IASTracePrintf ("Unable to shutdown SDO component");
  564. }
  565. g_pISdoService->Release ();
  566. g_pISdoService = NULL;
  567. }
  568. IASTracePrintf ("Surrogate Shutdown complete.");
  569. LeaveCriticalSection (&g_SrvCritSect);
  570. return;
  571. } // end of ShutdownIas method
  572. //++--------------------------------------------------------------
  573. //
  574. // Function: ConfigureIas
  575. //
  576. // Synopsis: This is the API that is called to reload the
  577. // IAS configuration information
  578. //
  579. // Arguments: none
  580. //
  581. // Returns: HRESULT - status
  582. //
  583. // History: MKarki Created 09/04/98
  584. //
  585. //----------------------------------------------------------------
  586. extern "C" HRESULT WINAPI
  587. ConfigureIas (
  588. VOID
  589. )
  590. {
  591. HRESULT hr = S_OK;
  592. EnterCriticalSection (&g_SrvCritSect);
  593. IASTracePrintf ("Configuring Surrogate.");
  594. if (FALSE == g_bInitialized)
  595. {
  596. IASTracePrintf (
  597. "InitializeIas needs to be called before configuring surrogate"
  598. );
  599. }
  600. else if (NULL != g_pISdoService)
  601. {
  602. hr = g_pISdoService->ConfigureService (SERVICE_TYPE_RAS);
  603. }
  604. LeaveCriticalSection (&g_SrvCritSect);
  605. return (hr);
  606. } // end of ConfigureIas method
  607. //++--------------------------------------------------------------
  608. //
  609. // Function: MemAllocIas
  610. //
  611. // Synopsis: This is the API used to allocate dynamic memory
  612. //
  613. // Arguments: none
  614. //
  615. // Returns: PVOID - address of allocated memory
  616. //
  617. // History: MKarki Created 2/10/98
  618. //
  619. //----------------------------------------------------------------
  620. extern "C" PVOID WINAPI
  621. MemAllocIas (
  622. DWORD dwSize
  623. )
  624. {
  625. return (::CoTaskMemAlloc (dwSize));
  626. } // end of MemAllocIas API
  627. //++--------------------------------------------------------------
  628. //
  629. // Function: MemFreeIas
  630. //
  631. // Synopsis: This is the API to free the dynamic memory
  632. // allocated through MemAllocIas
  633. //
  634. // Arguments: PVOID - address of allocated memory
  635. //
  636. // Returns: VOID
  637. //
  638. // History: MKarki Created 2/10/98
  639. //
  640. //----------------------------------------------------------------
  641. extern "C" VOID WINAPI
  642. MemFreeIas (
  643. PVOID pAllocMem
  644. )
  645. {
  646. ::CoTaskMemFree (pAllocMem);
  647. } // end of MemFreeIas API
  648. //++--------------------------------------------------------------
  649. //
  650. // Function: MemReallocIas
  651. //
  652. // Synopsis: This is the API to reallocate the already allocate
  653. // dynamic memory allocated through MemAllocIas
  654. //
  655. // Arguments: PVOID - address of allocated memory
  656. // DWORD - new size
  657. //
  658. // Returns: PVOID - adress of new memory
  659. //
  660. // History: MKarki Created 2/10/98
  661. //
  662. //----------------------------------------------------------------
  663. extern "C" PVOID WINAPI
  664. MemReallocIas (
  665. PVOID pAllocMem,
  666. DWORD dwNewSize
  667. )
  668. {
  669. return (::CoTaskMemRealloc (pAllocMem, dwNewSize));
  670. } // end of MemReallocIas API
  671. #include <atlimpl.cpp>