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.

328 lines
8.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 2000
  5. //
  6. // File: H N B R G C O N . C P P
  7. //
  8. // Contents: CHNBridgedConn implementation
  9. //
  10. // Notes:
  11. //
  12. // Author: jonburs 23 June 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. //
  18. // Ojbect initialization
  19. //
  20. HRESULT
  21. CHNBridgedConn::Initialize(
  22. IWbemServices *piwsNamespace,
  23. IWbemClassObject *pwcoConnection
  24. )
  25. {
  26. return InitializeFromConnection(piwsNamespace, pwcoConnection);
  27. }
  28. //
  29. // IHNetBridgedConnection methods
  30. //
  31. STDMETHODIMP
  32. CHNBridgedConn::GetBridge(
  33. IHNetBridge **ppBridge
  34. )
  35. {
  36. HRESULT hr;
  37. if (NULL != ppBridge)
  38. {
  39. *ppBridge = NULL;
  40. hr = GetBridgeConnection( m_piwsHomenet, ppBridge );
  41. }
  42. else
  43. {
  44. hr = E_POINTER;
  45. }
  46. return hr;
  47. }
  48. STDMETHODIMP
  49. CHNBridgedConn::RemoveFromBridge(
  50. IN OPTIONAL INetCfg *pnetcfgExisting
  51. )
  52. {
  53. HRESULT hr = S_OK;
  54. BSTR bstr;
  55. if (ProhibitedByPolicy(NCPERM_AllowNetBridge_NLA))
  56. {
  57. hr = HN_E_POLICY;
  58. }
  59. if (S_OK == hr)
  60. {
  61. //
  62. // Unbind ourselves from the bridge
  63. //
  64. hr = UnbindFromBridge( pnetcfgExisting );
  65. }
  66. if (S_OK == hr)
  67. {
  68. //
  69. // Inform netman that something changed. Error doesn't matter.
  70. //
  71. UpdateNetman();
  72. }
  73. return hr;
  74. }
  75. HRESULT
  76. CHNBridgedConn::CopyBridgeBindings(
  77. IN INetCfgComponent *pnetcfgAdapter,
  78. IN INetCfgComponent *pnetcfgBridge
  79. )
  80. {
  81. HRESULT hr = S_OK;
  82. INetCfgComponentBindings *pnetcfgAdapterBindings;
  83. //
  84. // Get the adapter's ComponentBindings interface
  85. //
  86. hr = pnetcfgAdapter->QueryInterface(
  87. IID_PPV_ARG(INetCfgComponentBindings, &pnetcfgAdapterBindings)
  88. );
  89. if (S_OK == hr)
  90. {
  91. IEnumNetCfgBindingPath *penumPaths;
  92. //
  93. // Get the list of binding paths for the adapter
  94. //
  95. hr = pnetcfgAdapterBindings->EnumBindingPaths(
  96. EBP_ABOVE,
  97. &penumPaths
  98. );
  99. if (S_OK == hr)
  100. {
  101. ULONG ulCount1, ulCount2;
  102. INetCfgBindingPath *pnetcfgPath;
  103. while( (S_OK == penumPaths->Next(1, &pnetcfgPath, &ulCount1) ) )
  104. {
  105. INetCfgComponent *pnetcfgOwner;
  106. //
  107. // Get the owner of this path
  108. //
  109. hr = pnetcfgPath->GetOwner( &pnetcfgOwner );
  110. if (S_OK == hr)
  111. {
  112. INetCfgComponentBindings *pnetcfgOwnerBindings;
  113. //
  114. // Need the ComponentBindings interface for the owner
  115. //
  116. hr = pnetcfgOwner->QueryInterface(
  117. IID_PPV_ARG(INetCfgComponentBindings, &pnetcfgOwnerBindings)
  118. );
  119. if (S_OK == hr)
  120. {
  121. LPWSTR lpwstrId;
  122. //
  123. // The rule is, the binding should be disabled if it
  124. // represents the bridge protocol or something that
  125. // is not bound to the bridge that the adapter is
  126. // coming out of.
  127. //
  128. // If the binding is one that the bridge has, it is
  129. // enabled.
  130. //
  131. // This makes the adapter's bindings mirror those of
  132. // the bridge it just left.
  133. //
  134. hr = pnetcfgOwner->GetId( &lpwstrId );
  135. if (S_OK == hr)
  136. {
  137. UINT cmp = _wcsicmp(lpwstrId, c_wszSBridgeSID);
  138. hr = pnetcfgOwnerBindings->IsBoundTo( pnetcfgBridge );
  139. if ( (S_OK == hr) && (cmp != 0) )
  140. {
  141. // Activate this binding path
  142. hr = pnetcfgOwnerBindings->BindTo(pnetcfgAdapter);
  143. }
  144. else
  145. {
  146. // Deactivate this path
  147. hr = pnetcfgOwnerBindings->UnbindFrom(pnetcfgAdapter);
  148. }
  149. CoTaskMemFree(lpwstrId);
  150. }
  151. pnetcfgOwnerBindings->Release();
  152. }
  153. pnetcfgOwner->Release();
  154. }
  155. pnetcfgPath->Release();
  156. }
  157. penumPaths->Release();
  158. }
  159. pnetcfgAdapterBindings->Release();
  160. }
  161. return hr;
  162. }
  163. HRESULT
  164. CHNBridgedConn::UnbindFromBridge(
  165. IN OPTIONAL INetCfg *pnetcfgExisting
  166. )
  167. {
  168. HRESULT hr = S_OK;
  169. GUID *pguidAdapter;
  170. INetCfg *pnetcfg = NULL;
  171. INetCfgLock *pncfglock = NULL;
  172. if( NULL == pnetcfgExisting )
  173. {
  174. hr = InitializeNetCfgForWrite( &pnetcfg, &pncfglock );
  175. // Bail out if we can't acquire NetCfg.
  176. if( FAILED(hr) )
  177. {
  178. return hr;
  179. }
  180. }
  181. else
  182. {
  183. // Use the NetCfg context we were given
  184. pnetcfg = pnetcfgExisting;
  185. }
  186. // We must have a NetCfg context at this point
  187. _ASSERT( pnetcfg != NULL );
  188. //
  189. // Get our own device GUID
  190. //
  191. hr = GetGuid (&pguidAdapter);
  192. if ( SUCCEEDED(hr) )
  193. {
  194. IHNetBridge *pbridge;
  195. //
  196. // Get our bridge
  197. //
  198. hr = GetBridge (&pbridge);
  199. if ( SUCCEEDED(hr) )
  200. {
  201. IHNetConnection *phnetconBridge;
  202. //
  203. // Get the bridge's IHNetConnection interface
  204. //
  205. hr = pbridge->QueryInterface(
  206. IID_PPV_ARG(IHNetConnection, &phnetconBridge)
  207. );
  208. if ( SUCCEEDED(hr) )
  209. {
  210. GUID *pguidBridge;
  211. // Get the bridge's device GUID
  212. hr = phnetconBridge->GetGuid (&pguidBridge);
  213. if ( SUCCEEDED(hr) )
  214. {
  215. INetCfgComponent *pnetcfgcompAdapter;
  216. hr = FindAdapterByGUID(
  217. pnetcfg,
  218. pguidAdapter,
  219. &pnetcfgcompAdapter
  220. );
  221. if ( SUCCEEDED(hr) )
  222. {
  223. INetCfgComponent *pnetcfgcompBridge;
  224. hr = FindAdapterByGUID(
  225. pnetcfg,
  226. pguidBridge,
  227. &pnetcfgcompBridge
  228. );
  229. if ( SUCCEEDED(hr) )
  230. {
  231. hr = CopyBridgeBindings(
  232. pnetcfgcompAdapter,
  233. pnetcfgcompBridge
  234. );
  235. pnetcfgcompBridge->Release();
  236. }
  237. pnetcfgcompAdapter->Release();
  238. }
  239. CoTaskMemFree(pguidBridge);
  240. }
  241. phnetconBridge->Release();
  242. }
  243. pbridge->Release();
  244. }
  245. CoTaskMemFree(pguidAdapter);
  246. }
  247. // If we created our own NetCfg context, shut it down now
  248. if( NULL == pnetcfgExisting )
  249. {
  250. // Apply everything if we succeeded, back out otherwise
  251. if ( SUCCEEDED(hr) )
  252. {
  253. hr = pnetcfg->Apply();
  254. // Refresh the UI for this connection
  255. RefreshNetConnectionsUI();
  256. }
  257. else
  258. {
  259. // Don't want to lose the original error code
  260. pnetcfg->Cancel();
  261. }
  262. UninitializeNetCfgForWrite( pnetcfg, pncfglock );
  263. }
  264. return hr;
  265. }