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.

411 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. CollectionChannels.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 "AlgController.h"
  14. #define NAT_PROTOCOL_TCP 0x06
  15. #define NAT_PROTOCOL_UDP 0x11
  16. CCollectionControlChannelsPrimary::~CCollectionControlChannelsPrimary()
  17. {
  18. RemoveAll();
  19. }
  20. //
  21. // Add a new control channel (Thread safe)
  22. //
  23. HRESULT
  24. CCollectionControlChannelsPrimary::Add(
  25. CPrimaryControlChannel* pChannelToAdd
  26. )
  27. {
  28. try
  29. {
  30. ENTER_AUTO_CS
  31. m_ListOfChannels.push_back(pChannelToAdd);
  32. g_pAlgController->m_CollectionOfAdapters.ApplyPrimaryChannel(pChannelToAdd);
  33. pChannelToAdd->AddRef();
  34. }
  35. catch(...)
  36. {
  37. return E_FAIL;
  38. }
  39. return S_OK;
  40. }
  41. //
  42. // Remove a channel from the list (Thead safe)
  43. //
  44. HRESULT
  45. CCollectionControlChannelsPrimary::Remove(
  46. CPrimaryControlChannel* pChannelToRemove
  47. )
  48. {
  49. HRESULT hr = S_OK;
  50. try
  51. {
  52. ENTER_AUTO_CS
  53. LISTOF_CHANNELS_PRIMARY::iterator theIterator = std::find(
  54. m_ListOfChannels.begin(),
  55. m_ListOfChannels.end(),
  56. pChannelToRemove
  57. );
  58. if ( *theIterator )
  59. {
  60. m_ListOfChannels.erase(theIterator); // Remove from list
  61. pChannelToRemove->CancelRedirects();
  62. pChannelToRemove->Release();
  63. }
  64. }
  65. catch(...)
  66. {
  67. return E_FAIL;
  68. }
  69. return hr;
  70. }
  71. //
  72. // Empty the list and free the PrimaryControlChannels
  73. //
  74. HRESULT
  75. CCollectionControlChannelsPrimary::RemoveAll()
  76. {
  77. try
  78. {
  79. ENTER_AUTO_CS
  80. MYTRACE_ENTER("CCollectionControlChannelsPrimary::RemoveAll()");
  81. //
  82. // By deleting all the ControlChannel they will also cancel all associated Redirects
  83. //
  84. MYTRACE("Collection has %d item", m_ListOfChannels.size());
  85. LISTOF_CHANNELS_PRIMARY::iterator theIterator;
  86. while ( m_ListOfChannels.size() > 0 )
  87. {
  88. theIterator = m_ListOfChannels.begin();
  89. m_ListOfChannels.erase(theIterator); // Remove from list
  90. (*theIterator)->CancelRedirects();
  91. (*theIterator)->Release();
  92. }
  93. }
  94. catch(...)
  95. {
  96. return E_FAIL;
  97. }
  98. return S_OK;
  99. }
  100. //
  101. // Set a dynamic redirection and all collected Primary ControlChannel
  102. //
  103. HRESULT
  104. CCollectionControlChannelsPrimary::SetRedirects(
  105. ALG_ADAPTER_TYPE eAdapterType,
  106. ULONG nAdapterIndex,
  107. ULONG nAdapterAddress
  108. )
  109. {
  110. HRESULT hr=S_OK;
  111. try
  112. {
  113. ENTER_AUTO_CS
  114. MYTRACE_ENTER("CCollectionControlChannelsPrimary::SetRedirects");
  115. MYTRACE("AdapterType %d, RealAdapterIndex %d, Currently %d ControlChannel in the collection", eAdapterType, nAdapterIndex, m_ListOfChannels.size());
  116. //
  117. // Set redirect for all Channel
  118. //
  119. for ( LISTOF_CHANNELS_PRIMARY::iterator theIterator = m_ListOfChannels.begin();
  120. theIterator != m_ListOfChannels.end();
  121. theIterator++
  122. )
  123. {
  124. (*theIterator)->SetRedirect(
  125. eAdapterType,
  126. nAdapterIndex,
  127. nAdapterAddress
  128. );
  129. }
  130. }
  131. catch(...)
  132. {
  133. hr = E_FAIL;
  134. }
  135. return hr;
  136. }
  137. //
  138. // Check to see if the any PrimaryChannel need to be apply or his redirect should be removed
  139. //
  140. HRESULT
  141. CCollectionControlChannelsPrimary::AdapterPortMappingChanged(
  142. ULONG nCookie,
  143. UCHAR ucProtocol,
  144. USHORT usPort
  145. )
  146. {
  147. HRESULT hr = S_OK;
  148. ALG_PROTOCOL algProtocol;
  149. MYTRACE_ENTER("CCollectionControlChannelsPrimary::AdapterPortMappingChanged");
  150. MYTRACE("AdapterCookie %d, Protocol %d, Port %d", nCookie, ucProtocol, usPort);
  151. if (NAT_PROTOCOL_TCP == ucProtocol)
  152. {
  153. algProtocol = eALG_TCP;
  154. }
  155. else if (NAT_PROTOCOL_UDP == ucProtocol)
  156. {
  157. algProtocol = eALG_UDP;
  158. }
  159. else
  160. {
  161. hr = E_INVALIDARG;
  162. }
  163. if (SUCCEEDED(hr))
  164. {
  165. try
  166. {
  167. ENTER_AUTO_CS
  168. CPrimaryControlChannel *pControlChannel = FindControlChannel(algProtocol, usPort);
  169. if (NULL != pControlChannel
  170. && pControlChannel->m_Properties.fCaptureInbound)
  171. {
  172. hr = g_pAlgController->m_CollectionOfAdapters.AdapterUpdatePrimaryChannel(
  173. nCookie,
  174. pControlChannel
  175. );
  176. }
  177. }
  178. catch (...)
  179. {
  180. hr = E_FAIL;
  181. }
  182. }
  183. return hr;
  184. }
  185. //
  186. // Called when an adapter got removed
  187. // function will cancel any redirect that was done on this adapter index
  188. //
  189. HRESULT
  190. CCollectionControlChannelsPrimary::AdapterRemoved(
  191. ULONG nAdapterIndex
  192. )
  193. {
  194. HRESULT hr = S_OK;
  195. MYTRACE_ENTER("CCollectionControlChannelsPrimary::AdapterRemoved");
  196. MYTRACE("AdapterIndex %d", nAdapterIndex);
  197. try
  198. {
  199. ENTER_AUTO_CS
  200. //
  201. // Set redirect for all Channel
  202. //
  203. for ( LISTOF_CHANNELS_PRIMARY::iterator theIterator = m_ListOfChannels.begin();
  204. theIterator != m_ListOfChannels.end();
  205. theIterator++
  206. )
  207. {
  208. (*theIterator)->CancelRedirectsForAdapter(
  209. nAdapterIndex
  210. );
  211. }
  212. }
  213. catch (...)
  214. {
  215. hr = E_FAIL;
  216. }
  217. return hr;
  218. }
  219. //
  220. //
  221. // Collection of Secondary control channels
  222. //
  223. //
  224. //
  225. //
  226. //
  227. CCollectionControlChannelsSecondary::~CCollectionControlChannelsSecondary()
  228. {
  229. RemoveAll();
  230. }
  231. //
  232. // Add a new control channel (Thread safe)
  233. //
  234. HRESULT
  235. CCollectionControlChannelsSecondary::Add(
  236. CSecondaryControlChannel* pChannelToAdd
  237. )
  238. {
  239. try
  240. {
  241. ENTER_AUTO_CS
  242. m_ListOfChannels.push_back(pChannelToAdd);
  243. pChannelToAdd->AddRef();
  244. }
  245. catch(...)
  246. {
  247. return E_FAIL;
  248. }
  249. return S_OK;
  250. }
  251. //
  252. // Remove a channel from the list (Thead safe)
  253. //
  254. HRESULT
  255. CCollectionControlChannelsSecondary::Remove(
  256. CSecondaryControlChannel* pChannelToRemove
  257. )
  258. {
  259. try
  260. {
  261. ENTER_AUTO_CS
  262. LISTOF_CHANNELS_SECONDARY::iterator theIterator = std::find(
  263. m_ListOfChannels.begin(),
  264. m_ListOfChannels.end(),
  265. pChannelToRemove
  266. );
  267. if ( *theIterator )
  268. {
  269. m_ListOfChannels.erase(theIterator); // Remove from list
  270. pChannelToRemove->CancelRedirects();
  271. pChannelToRemove->Release();
  272. }
  273. }
  274. catch(...)
  275. {
  276. return E_FAIL;
  277. }
  278. return S_OK;
  279. }
  280. //
  281. // When a Control is cancel it need to Cancel all it's redirect previousely created
  282. //
  283. HRESULT
  284. CCollectionControlChannelsSecondary::RemoveAll()
  285. {
  286. try
  287. {
  288. ENTER_AUTO_CS
  289. MYTRACE_ENTER("CCollectionControlChannelsSecondary::RemoveAll()");
  290. //
  291. // By deleting all the SecondaryControlChannel they will also cancel all associated Redirects
  292. //
  293. MYTRACE("Collection has %d item", m_ListOfChannels.size());
  294. LISTOF_CHANNELS_SECONDARY::iterator theIterator;
  295. while ( m_ListOfChannels.size() > 0 )
  296. {
  297. theIterator = m_ListOfChannels.begin();
  298. m_ListOfChannels.erase(theIterator); // Remove from list
  299. (*theIterator)->CancelRedirects();
  300. (*theIterator)->Release();
  301. }
  302. }
  303. catch(...)
  304. {
  305. return E_FAIL;
  306. }
  307. return S_OK;
  308. }