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.

469 lines
11 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. CollectionAdapters.cpp
  5. Abstract:
  6. Implement a collection of the CPrimaryControlChannel.cpp & CSecondaryControlChannel
  7. in a threa safe way.
  8. Author:
  9. JP Duplessis (jpdup) 08-Dec-2000
  10. Revision History:
  11. --*/
  12. #include "PreComp.h"
  13. #include "CollectionAdapters.h"
  14. #include "CollectionAdapterNotifySinks.h"
  15. #include "AlgController.h"
  16. CCollectionAdapters::~CCollectionAdapters()
  17. {
  18. RemoveAll();
  19. }
  20. //
  21. // Add an already created Adapter
  22. //
  23. HRESULT
  24. CCollectionAdapters::Add(
  25. IAdapterInfo* pAdapterToAdd
  26. )
  27. {
  28. try
  29. {
  30. ENTER_AUTO_CS
  31. if ( !FindUsingInterface(pAdapterToAdd) )
  32. m_ListOfAdapters.push_back(pAdapterToAdd);
  33. }
  34. catch(...)
  35. {
  36. return E_FAIL;
  37. }
  38. return S_OK;
  39. }
  40. //
  41. // Add a NEW Adapter this function insure that the is only 1 adatper with the given INDEX
  42. // returns the newly added adapter or NULL is faild
  43. // if the Adapter index was already present the return pointer is of the one found in the collection
  44. //
  45. IAdapterInfo*
  46. CCollectionAdapters::Add(
  47. IN ULONG nCookie,
  48. IN short nType
  49. )
  50. {
  51. CComObject<CAdapterInfo>* pIAdapterInfo=NULL;
  52. try
  53. {
  54. ENTER_AUTO_CS
  55. MYTRACE_ENTER("CCollectionAdapters::Add");
  56. MYTRACE("Adapter Cookie %d of type %d", nCookie, nType);
  57. IAdapterInfo* pIFound = FindUsingCookie(nCookie);
  58. if ( pIFound )
  59. return pIFound; // Adapter with the given Index is already in the collection
  60. HRESULT hr = CComObject<CAdapterInfo>::CreateInstance(&pIAdapterInfo);
  61. if ( FAILED(hr) )
  62. {
  63. MYTRACE_ERROR("CComObject<CAdapterInfo>::CreateInstance(&pIAdapterInfo)",hr);
  64. return NULL; //ERROR_NOT_ENOUGH_MEMORY;
  65. }
  66. //
  67. // Initialize the new interface
  68. //
  69. pIAdapterInfo->m_nCookie = nCookie;
  70. pIAdapterInfo->m_eType = (ALG_ADAPTER_TYPE)nType;
  71. m_ListOfAdapters.push_back(pIAdapterInfo);
  72. pIAdapterInfo->AddRef();
  73. }
  74. catch(...)
  75. {
  76. return NULL;
  77. }
  78. return pIAdapterInfo;
  79. }
  80. //
  81. // Remove a adapter from the list (Thead safe)
  82. //
  83. HRESULT
  84. CCollectionAdapters::Remove(
  85. IAdapterInfo* pAdapterToRemove
  86. )
  87. {
  88. try
  89. {
  90. ENTER_AUTO_CS
  91. MYTRACE_ENTER("CCollectionAdapters::Remove by IAdapterInfo");
  92. LISTOF_ADAPTERS::iterator theIterator = std::find(
  93. m_ListOfAdapters.begin(),
  94. m_ListOfAdapters.end(),
  95. pAdapterToRemove
  96. );
  97. if ( *theIterator )
  98. {
  99. CAdapterInfo* pAdapterInfo = (CAdapterInfo*)pAdapterToRemove;
  100. g_pAlgController->m_AdapterNotificationSinks.Notify(eNOTIFY_REMOVED, (*theIterator) );
  101. g_pAlgController->m_ControlChannelsPrimary.AdapterRemoved(pAdapterInfo->m_nAdapterIndex);
  102. (*theIterator)->Release();
  103. m_ListOfAdapters.erase(theIterator);
  104. }
  105. }
  106. catch(...)
  107. {
  108. return E_FAIL;
  109. }
  110. return S_OK;
  111. }
  112. //
  113. // Remove a adapter from the list (Thead safe)
  114. //
  115. HRESULT
  116. CCollectionAdapters::Remove(
  117. ULONG nCookieOfAdapterToRemove
  118. )
  119. {
  120. try
  121. {
  122. ENTER_AUTO_CS
  123. MYTRACE_ENTER("CCollectionAdapters::Remove by Index");
  124. for ( LISTOF_ADAPTERS::iterator theIterator = m_ListOfAdapters.begin();
  125. theIterator != m_ListOfAdapters.end();
  126. theIterator++
  127. )
  128. {
  129. CAdapterInfo* pAdapterInfo = (CAdapterInfo*)(*theIterator);
  130. if ( pAdapterInfo->m_nCookie == nCookieOfAdapterToRemove )
  131. {
  132. g_pAlgController->m_AdapterNotificationSinks.Notify(eNOTIFY_REMOVED, (*theIterator) );
  133. g_pAlgController->m_ControlChannelsPrimary.AdapterRemoved(pAdapterInfo->m_nAdapterIndex);
  134. pAdapterInfo->Release();
  135. m_ListOfAdapters.erase(theIterator);
  136. return S_OK;
  137. }
  138. }
  139. }
  140. catch(...)
  141. {
  142. return E_FAIL;
  143. }
  144. return E_INVALIDARG;
  145. }
  146. //
  147. // When an adapter form the collection
  148. //
  149. HRESULT
  150. CCollectionAdapters::RemoveAll()
  151. {
  152. try
  153. {
  154. ENTER_AUTO_CS
  155. MYTRACE_ENTER("CCollectionAdapters::RemoveAll");
  156. //
  157. // By deleting all the ControlChannel they will also cancel all their associated Redirection
  158. //
  159. MYTRACE("Collection has %d item", m_ListOfAdapters.size());
  160. LISTOF_ADAPTERS::iterator theIterator;
  161. while ( m_ListOfAdapters.size() > 0 )
  162. {
  163. theIterator = m_ListOfAdapters.begin();
  164. CAdapterInfo* pAdapterInfo = (CAdapterInfo*)(*theIterator);
  165. pAdapterInfo->Release();
  166. m_ListOfAdapters.erase(theIterator);
  167. }
  168. }
  169. catch(...)
  170. {
  171. return E_FAIL;
  172. }
  173. return S_OK;
  174. }
  175. //
  176. // Return an IAdapterInfo the caller is responsable of releasing the interface
  177. //
  178. HRESULT
  179. CCollectionAdapters::GetAdapterInfo(
  180. IN ULONG nAdapterIndex,
  181. OUT IAdapterInfo** ppAdapterInfo
  182. )
  183. {
  184. MYTRACE_ENTER("CCollectionAdapters::GetAdapterInfo");
  185. try
  186. {
  187. ENTER_AUTO_CS
  188. MYTRACE("Adapter index %d requested", nAdapterIndex);
  189. *ppAdapterInfo = FindUsingAdapterIndex(nAdapterIndex);
  190. if ( *ppAdapterInfo == NULL )
  191. {
  192. MYTRACE_ERROR("Adapter was not found in the collection", 0);
  193. return E_INVALIDARG;
  194. }
  195. (*ppAdapterInfo)->AddRef();
  196. }
  197. catch(...)
  198. {
  199. MYTRACE_ERROR("TRY/CATCH",0);
  200. return E_FAIL;
  201. }
  202. return S_OK;
  203. }
  204. //
  205. // Update the addresses member proprety
  206. //
  207. // Now that we have the address we can apply any outstanding ControlChannel (Redirect)
  208. //
  209. HRESULT
  210. CCollectionAdapters::SetAddresses(
  211. ULONG nCookie,
  212. ULONG nAdapterIndex,
  213. ULONG nAddressCount,
  214. DWORD anAddress[]
  215. )
  216. {
  217. try
  218. {
  219. ENTER_AUTO_CS
  220. MYTRACE_ENTER("CCollectionAdapters::SetAddresses");
  221. MYTRACE("Adapter BIND Cookie %d Address Count %d", nCookie, nAddressCount);
  222. CAdapterInfo* pIAdapterFound = (CAdapterInfo*)FindUsingCookie(nCookie);
  223. if ( !pIAdapterFound )
  224. {
  225. MYTRACE_ERROR("Adapter was not found in the collection", 0);
  226. return E_INVALIDARG;
  227. }
  228. //
  229. // Cache the Adapter Index
  230. //
  231. pIAdapterFound->m_nAdapterIndex = nAdapterIndex;
  232. //
  233. // Cache the addresses
  234. //
  235. pIAdapterFound->m_nAddressCount = nAddressCount;
  236. for ( short nA=0; nA < nAddressCount; nA++ )
  237. pIAdapterFound->m_anAddress[nA] = anAddress[nA];
  238. //
  239. // Fire any Sink that may be setup
  240. //
  241. if ( pIAdapterFound->m_bNotified )
  242. {
  243. //
  244. // Already notify once the user that this adapter was added
  245. // from now on any CCollectionAdapters::SetAddresses
  246. // will trigger a eNOTIFY_MODIFIED notification
  247. //
  248. g_pAlgController->m_AdapterNotificationSinks.Notify(
  249. eNOTIFY_MODIFIED,
  250. pIAdapterFound
  251. );
  252. }
  253. else
  254. {
  255. //
  256. // Ok this is the first time we received address for this
  257. // adapter we will let the user know that a new adapter got added
  258. //
  259. g_pAlgController->m_AdapterNotificationSinks.Notify(
  260. eNOTIFY_ADDED,
  261. pIAdapterFound
  262. );
  263. pIAdapterFound->m_bNotified = true;
  264. }
  265. //
  266. // Create redirect(s) for any ControlChannels in the Collection of PrimaryControlChannel
  267. //
  268. g_pAlgController->m_ControlChannelsPrimary.SetRedirects(
  269. pIAdapterFound->m_eType,
  270. nAdapterIndex,
  271. anAddress[0]
  272. );
  273. }
  274. catch(...)
  275. {
  276. return E_FAIL;
  277. }
  278. return S_OK;
  279. }
  280. //
  281. //
  282. //
  283. HRESULT
  284. CCollectionAdapters::ApplyPrimaryChannel(
  285. CPrimaryControlChannel* pChannelToActivate
  286. )
  287. {
  288. MYTRACE_ENTER("CCollectionAdapters::ApplyPrimaryChannel");
  289. if ( !pChannelToActivate )
  290. return E_INVALIDARG;
  291. ENTER_AUTO_CS
  292. for ( LISTOF_ADAPTERS::iterator theIterator = m_ListOfAdapters.begin();
  293. theIterator != m_ListOfAdapters.end();
  294. theIterator++
  295. )
  296. {
  297. CAdapterInfo* pAdapterInfo = (CAdapterInfo*)(*theIterator);
  298. if ( pAdapterInfo && pAdapterInfo->m_nAddressCount >0 )
  299. {
  300. pChannelToActivate->SetRedirect(
  301. pAdapterInfo->m_eType,
  302. pAdapterInfo->m_nAdapterIndex,
  303. pAdapterInfo->m_nAddressCount
  304. );
  305. }
  306. }
  307. return S_OK;
  308. }
  309. //
  310. // Will be called when port mapping has changed
  311. //
  312. HRESULT
  313. CCollectionAdapters::AdapterUpdatePrimaryChannel(
  314. ULONG nCookie,
  315. CPrimaryControlChannel *pChannel
  316. )
  317. {
  318. HRESULT hr = S_OK;
  319. MYTRACE_ENTER("CCollectionAdapters::AdapterUpdatePrimaryChannel");
  320. try
  321. {
  322. ENTER_AUTO_CS
  323. CAdapterInfo *pAdapter = (CAdapterInfo*) FindUsingCookie(nCookie);
  324. if (NULL != pAdapter
  325. && ( (eALG_BOUNDARY & pAdapter->m_eType) ||
  326. (eALG_FIREWALLED & pAdapter->m_eType)
  327. )
  328. )
  329. {
  330. ULONG ulAddress;
  331. USHORT usPort;
  332. HANDLE_PTR hRedirect;
  333. HRESULT hrPortMappingExists =
  334. g_pAlgController->GetNat()->LookupAdapterPortMapping(
  335. pAdapter->m_nAdapterIndex,
  336. pChannel->m_Properties.eProtocol,
  337. 0,
  338. pChannel->m_Properties.usCapturePort,
  339. &ulAddress,
  340. &usPort
  341. );
  342. hRedirect = pChannel->m_CollectionRedirects.FindInboundRedirect(pAdapter->m_nAdapterIndex);
  343. if (SUCCEEDED(hrPortMappingExists) && NULL == hRedirect)
  344. {
  345. MYTRACE("PortMapping Exist and We had no Redirect so create them");
  346. hr = pChannel->CreateInboundRedirect(pAdapter->m_nAdapterIndex);
  347. }
  348. else if (FAILED(hrPortMappingExists) && NULL != hRedirect)
  349. {
  350. MYTRACE("PortMapping DOES NOT Exist and We had Redirect set so remove them");
  351. hr = pChannel->m_CollectionRedirects.Remove(hRedirect);
  352. }
  353. }
  354. else
  355. {
  356. MYTRACE("Adapter is not ICS or ICF");
  357. }
  358. }
  359. catch (...)
  360. {
  361. hr = E_FAIL;
  362. }
  363. return hr;
  364. }