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.

626 lines
12 KiB

  1. /*++
  2. Copyright (c) 1998 - 2000 Microsoft Corporation
  3. Module Name:
  4. main.cpp
  5. Abstract:
  6. Contains:
  7. 1. Module startup routines
  8. 2. Component activation routines
  9. 3. Component deactivation routines
  10. 4. Module shutdown/cleanup routines
  11. 5. Auxiliary routines
  12. Revision History:
  13. 1. 31-Jul-1998 -- File creation Ajay Chitturi (ajaych)
  14. 2. 15-Jul-1999 -- Arlie Davis (arlied)
  15. 3. 14-Feb-2000 -- Added support for multiple Ilya Kleyman (ilyak)
  16. private interfaces
  17. --*/
  18. #include "stdafx.h"
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // //
  21. // Global Variables //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. HANDLE NatHandle = NULL;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // //
  27. // Static declarations //
  28. // //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. static
  31. void
  32. QueryRegistry (
  33. void
  34. );
  35. static
  36. HRESULT
  37. H323ProxyStartServiceInternal (
  38. void
  39. );
  40. static
  41. HRESULT
  42. H323ProxyStart (
  43. void
  44. );
  45. static
  46. HRESULT
  47. LdapProxyStart (
  48. void
  49. );
  50. static
  51. void
  52. H323ProxyStop (
  53. void
  54. );
  55. static
  56. void
  57. LdapProxyStop (
  58. void
  59. );
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // //
  62. // Module startup routines //
  63. // //
  64. ///////////////////////////////////////////////////////////////////////////////
  65. EXTERN_C
  66. BOOLEAN
  67. H323ProxyInitializeModule (
  68. void
  69. )
  70. /*++
  71. Routine Description:
  72. Initializes module.
  73. Arguments:
  74. None
  75. Return Values:
  76. TRUE - if initialization was successful
  77. FALSE - if initialization failed
  78. Notes:
  79. Equivalent to DLL_PROCESS_ATTACH
  80. --*/
  81. {
  82. Debug (_T("H323: DLL_PROCESS_ATTACH.\n"));
  83. H323ASN1Initialize();
  84. return TRUE;
  85. } // H323ProxyInitializeModule
  86. EXTERN_C
  87. ULONG
  88. H323ProxyStartService (
  89. void
  90. )
  91. /*++
  92. Routine Description:
  93. Starts the service
  94. Arguments:
  95. None
  96. Return Values:
  97. Win32 error code
  98. Notes:
  99. Module entry point
  100. --*/
  101. {
  102. HRESULT Result;
  103. Debug (_T("H323: starting...\n"));
  104. Result = H323ProxyStartServiceInternal();
  105. if (Result == S_OK) {
  106. DebugF (_T("H323: H.323/LDAP proxy has initialized successfully.\n"));
  107. return ERROR_SUCCESS;
  108. }
  109. else {
  110. DebugError (Result, _T("H323: H.323/LDAP proxy has FAILED to initialize.\n"));
  111. return ERROR_CAN_NOT_COMPLETE;
  112. }
  113. } // H323ProxyStartService
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // //
  116. // Component activation routines //
  117. // //
  118. ///////////////////////////////////////////////////////////////////////////////
  119. static
  120. HRESULT
  121. H323ProxyStartServiceInternal (
  122. void
  123. )
  124. /*++
  125. Routine Description:
  126. Initializes all components.
  127. Arguments:
  128. None.
  129. Return Values:
  130. Returns S_OK in case of success or an error in case of failure.
  131. Notes:
  132. Internal version of DLL entry point
  133. --*/
  134. {
  135. WSADATA WsaData;
  136. HRESULT Result;
  137. QueryRegistry ();
  138. // Initialize WinSock
  139. Result = WSAStartup (MAKEWORD (2, 0), &WsaData);
  140. if (S_OK == Result) {
  141. // Initialize allocator of call reference values
  142. Result = InitCrvAllocator();
  143. if (S_OK == Result) {
  144. // initialize NAT
  145. Result = NatInitializeTranslator (&NatHandle);
  146. if (S_OK == Result) {
  147. // Initialize Port Pool
  148. Result = PortPoolStart ();
  149. if (S_OK == Result) {
  150. // Initialize H.323 Proxy
  151. Result = H323ProxyStart ();
  152. if (S_OK == Result) {
  153. // Initialize LDAP Proxy
  154. Result = LdapProxyStart ();
  155. if (S_OK == Result) {
  156. return S_OK;
  157. }
  158. H323ProxyStop ();
  159. }
  160. PortPoolStop ();
  161. }
  162. NatShutdownTranslator (NatHandle);
  163. NatHandle = NULL;
  164. }
  165. CleanupCrvAllocator ();
  166. }
  167. WSACleanup ();
  168. }
  169. return Result;
  170. } // H323ProxyStartServiceInternal
  171. HRESULT H323ProxyStart (
  172. void
  173. )
  174. /*++
  175. Routine Description:
  176. Initializes components of H.323 proxy
  177. Arguments:
  178. None
  179. Return Values:
  180. S_OK if successful, error code otherwise.
  181. Notes:
  182. --*/
  183. {
  184. HRESULT Result;
  185. Result = Q931SyncCounter.Start ();
  186. if (S_OK == Result) {
  187. CallBridgeList.Start ();
  188. Result = Q931CreateBindSocket ();
  189. if (S_OK == Result) {
  190. Result = Q931StartLoopbackRedirect ();
  191. if (S_OK == Result) {
  192. return S_OK;
  193. }
  194. Q931CloseSocket ();
  195. CallBridgeList.Stop ();
  196. }
  197. Q931SyncCounter.Wait (INFINITE);
  198. Q931SyncCounter.Stop ();
  199. }
  200. return Result;
  201. } // H323ProxyStart
  202. HRESULT LdapProxyStart (
  203. void
  204. )
  205. /*++
  206. Routine Description:
  207. Initializes components of LDAP proxy
  208. Arguments:
  209. None
  210. Return Values:
  211. S_OK if successful, error code otherwise
  212. Notes:
  213. --*/
  214. {
  215. HRESULT Status;
  216. Status = LdapSyncCounter.Start ();
  217. if (S_OK == Status) {
  218. Status = LdapCoder.Start();
  219. if (S_OK == Status) {
  220. Status = LdapTranslationTable.Start ();
  221. if (S_OK == Status) {
  222. LdapConnectionArray.Start ();
  223. Status = LdapAccept.Start ();
  224. if (S_OK == Status) {
  225. return S_OK;
  226. }
  227. LdapConnectionArray.Stop ();
  228. LdapTranslationTable.Stop ();
  229. }
  230. LdapCoder.Stop ();
  231. }
  232. LdapSyncCounter.Wait (INFINITE);
  233. LdapSyncCounter.Stop ();
  234. }
  235. return Status;
  236. } // LdapProxyStart
  237. EXTERN_C ULONG
  238. H323ProxyActivateInterface(
  239. IN ULONG Index,
  240. IN H323_INTERFACE_TYPE InterfaceType,
  241. IN PIP_ADAPTER_BINDING_INFO BindingInfo
  242. )
  243. /*++
  244. Routine Description:
  245. Activates an interface for H.323/LDAP
  246. Arguments:
  247. Index - Interface index (for internal use)
  248. BindingInfo - Interface binding information
  249. Return Values:
  250. Win32 error code
  251. Notes:
  252. Module entry point
  253. --*/
  254. {
  255. ULONG Error;
  256. DebugF (_T("H323: Request to activate interface with adapter index %d.\n"),
  257. Index);
  258. if (!BindingInfo->AddressCount ||
  259. !BindingInfo->Address[0].Address ||
  260. Index == INVALID_INTERFACE_INDEX) {
  261. return ERROR_INVALID_PARAMETER;
  262. }
  263. Error = InterfaceArray.AddStartInterface (Index, InterfaceType, BindingInfo);
  264. return Error;
  265. } // H323ProxyActivateInterface
  266. ///////////////////////////////////////////////////////////////////////////////
  267. // //
  268. // Module shutdown routines //
  269. // //
  270. ///////////////////////////////////////////////////////////////////////////////
  271. EXTERN_C void H323ProxyCleanupModule (
  272. void
  273. )
  274. /*++
  275. Routine Description:
  276. Shuts down module
  277. Arguments:
  278. None
  279. Return Values:
  280. None
  281. Notes:
  282. Equivalent to DLL_PROCESS_DETACH
  283. --*/
  284. {
  285. Debug (_T("H323: DLL_PROCESS_DETACH\n"));
  286. H323ASN1Shutdown ();
  287. } // H323ProxyCleanupModule
  288. EXTERN_C
  289. void H323ProxyStopService (
  290. void
  291. )
  292. /*++
  293. Routine Description:
  294. Stops the service
  295. Arguments:
  296. None
  297. Return Values:
  298. None
  299. Notes:
  300. Module entry point
  301. --*/
  302. {
  303. LdapProxyStop ();
  304. H323ProxyStop ();
  305. InterfaceArray.AssertShutdownReady ();
  306. InterfaceArray.Stop ();
  307. PortPoolStop ();
  308. if (NatHandle) {
  309. NatShutdownTranslator (NatHandle);
  310. NatHandle = NULL;
  311. }
  312. CleanupCrvAllocator ();
  313. WSACleanup ();
  314. Debug (_T("H323: service has stopped\n"));
  315. } // H323ProxyStopService
  316. void
  317. H323ProxyStop (
  318. void
  319. )
  320. /*++
  321. Routine Description:
  322. Stops H.323 proxy and waits until all call-bridges are deleted.
  323. Arguments:
  324. None
  325. Return Values:
  326. None
  327. Notes:
  328. --*/
  329. {
  330. Q931StopLoopbackRedirect ();
  331. Q931CloseSocket ();
  332. CallBridgeList.Stop ();
  333. Q931SyncCounter.Wait (INFINITE);
  334. Q931SyncCounter.Stop ();
  335. } // H323ProxyStop
  336. void
  337. LdapProxyStop (
  338. void)
  339. /*++
  340. Routine Description:
  341. LdapProxyStop is responsible for undoing all of the work that LdapProxyStart performed.
  342. It deletes the NAT redirect, deletes all LDAP connections (or, at least, it releases them
  343. -- they may not delete themselves yet if they have pending I/O or timer callbacks),
  344. and disables the creation of new LDAP connections.
  345. Arguments:
  346. None
  347. Return Values:
  348. None
  349. Notes:
  350. --*/
  351. {
  352. LdapAccept.Stop ();
  353. LdapConnectionArray.Stop ();
  354. LdapTranslationTable.Stop ();
  355. LdapCoder.Stop();
  356. LdapSyncCounter.Wait (INFINITE);
  357. LdapSyncCounter.Stop ();
  358. } // LdapProxyStop
  359. ///////////////////////////////////////////////////////////////////////////////
  360. // //
  361. // Component deactivation routines //
  362. // //
  363. ///////////////////////////////////////////////////////////////////////////////
  364. EXTERN_C
  365. VOID
  366. H323ProxyDeactivateInterface (
  367. IN ULONG Index
  368. )
  369. /*++
  370. Routine Description:
  371. Deactivates interface for H.323/LDAP
  372. Arguments:
  373. Index -- Interface index, previously passed to the
  374. interface activation routine
  375. Return Values:
  376. None
  377. Notes:
  378. Module entry point
  379. --*/
  380. {
  381. DebugF (_T("H323: DeactivateInterface called, index %d\n"),
  382. Index);
  383. InterfaceArray.RemoveStopInterface (Index);
  384. } // H323ProxyDeactivateInterface
  385. ///////////////////////////////////////////////////////////////////////////////
  386. // //
  387. // Auxiliary routines //
  388. // //
  389. ///////////////////////////////////////////////////////////////////////////////
  390. static
  391. void
  392. QueryRegistry (
  393. void
  394. )
  395. /*++
  396. Routine Description:
  397. Queries Registry for the values needed in module operations
  398. Arguments:
  399. None
  400. Return Values:
  401. None
  402. Notes:
  403. static
  404. --*/
  405. {
  406. HKEY Key;
  407. HRESULT Result;
  408. Result = RegOpenKeyEx (
  409. HKEY_LOCAL_MACHINE, H323ICS_SERVICE_PARAMETERS_KEY_PATH,
  410. 0,
  411. KEY_READ,
  412. &Key
  413. );
  414. if (ERROR_SUCCESS == Result) {
  415. Result = RegQueryValueDWORD (Key, H323ICS_REG_VAL_LOCAL_H323_ROUTING, &EnableLocalH323Routing);
  416. if (ERROR_SUCCESS != Result) {
  417. EnableLocalH323Routing = FALSE;
  418. }
  419. RegCloseKey (Key);
  420. } else {
  421. EnableLocalH323Routing = FALSE;
  422. }
  423. DebugF (_T("H323: Local H323 routing is %sabled.\n"),
  424. EnableLocalH323Routing ? _T("en") : _T("dis"));
  425. } // QueryRegistry