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.

427 lines
9.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 2000
  5. //
  6. // File: N E T I P. C P P
  7. //
  8. // Contents: Routines supporting RAS interoperability
  9. //
  10. // Notes:
  11. //
  12. // Author: billi 07 03 2001
  13. //
  14. // History:
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <windows.h>
  18. #include <devguid.h>
  19. #include <netcon.h>
  20. #include "netconn.h"
  21. #include "NetIp.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. // Prototype for iphlpapi routine. For some reason, this isn't defined
  25. // in any header.
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef DWORD (APIENTRY *LPFNSETADAPTERIPADDRESS)(
  30. LPSTR AdapterName,
  31. BOOL EnableDHCP,
  32. ULONG IPAddress,
  33. ULONG SubnetMask,
  34. ULONG DefaultGateway
  35. );
  36. #ifdef __cplusplus
  37. }
  38. #endif
  39. HRESULT HrSetAdapterIpAddress(
  40. LPSTR AdapterName,
  41. BOOL EnableDHCP,
  42. ULONG IPAddress,
  43. ULONG SubnetMask,
  44. ULONG DefaultGateway )
  45. //+---------------------------------------------------------------------------
  46. //
  47. // Function: HrSetAdapterIpAddress
  48. //
  49. // Purpose:
  50. //
  51. // Arguments:
  52. // LPSTR AdapterName,
  53. // BOOL EnableDHCP,
  54. // ULONG IPAddress,
  55. // ULONG SubnetMask,
  56. // ULONG DefaultGateway
  57. //
  58. // Returns: S_OK on success, otherwise an error code
  59. //
  60. // Notes:
  61. //
  62. {
  63. HRESULT hr = E_INVALIDARG;
  64. ASSERT( AdapterName );
  65. if ( AdapterName )
  66. {
  67. HMODULE hIpHlpApi = LoadLibrary(L"iphlpapi.dll");
  68. hr = E_FAIL;
  69. if ( hIpHlpApi )
  70. {
  71. LPFNSETADAPTERIPADDRESS pfnSetAdapterIpAddress =
  72. reinterpret_cast<LPFNSETADAPTERIPADDRESS>
  73. (GetProcAddress( hIpHlpApi, "SetAdapterIpAddress" ));
  74. if ( pfnSetAdapterIpAddress )
  75. {
  76. DWORD dwStatus = (*pfnSetAdapterIpAddress)(
  77. AdapterName, EnableDHCP, IPAddress, SubnetMask, DefaultGateway );
  78. hr = ( dwStatus ) ? HrFromWin32Error( dwStatus ) : S_OK;
  79. TraceMsg(TF_ALWAYS, "SetAdapterIpAddress = %lx hr = %lx", dwStatus, hr );
  80. }
  81. else
  82. {
  83. TraceMsg(TF_ALWAYS, "GetProcAddress( hIpHlpApi, SetAdapterIpAddress ) FAILED!" );
  84. }
  85. }
  86. else
  87. {
  88. TraceMsg(TF_ALWAYS, "LoadLibrary( iphlpapi.dll ) FAILED!" );
  89. }
  90. }
  91. TraceMsg(TF_ALWAYS, "HrSetAdapterIpAddress = %lx", hr );
  92. return hr;
  93. }
  94. DWORD GetInterfaceInformation( OUT PIP_INTERFACE_INFO * ppInterfaceInfo )
  95. //+---------------------------------------------------------------------------
  96. //
  97. // Function: GetInterfaceInformation
  98. //
  99. // Purpose:
  100. //
  101. // Arguments: PIP_INTERFACE_INFO * ppInterfaceInfo
  102. //
  103. // Returns: Status
  104. //
  105. // Notes:
  106. //
  107. {
  108. ASSERT( NULL != ppInterfaceInfo );
  109. LPBYTE pBuffer = NULL;
  110. DWORD dwBufferSize = 2048;
  111. DWORD dwStatus = 0;
  112. for ( int i=0; i<2; i++ )
  113. {
  114. pBuffer = new BYTE[ dwBufferSize ];
  115. if ( NULL != pBuffer )
  116. {
  117. dwStatus = GetInterfaceInfo( (PIP_INTERFACE_INFO) pBuffer, &dwBufferSize );
  118. if ( ERROR_INSUFFICIENT_BUFFER == dwStatus )
  119. {
  120. if ( NULL != pBuffer )
  121. {
  122. delete [] pBuffer;
  123. pBuffer = NULL;
  124. }
  125. }
  126. else
  127. {
  128. break;
  129. }
  130. }
  131. else
  132. {
  133. dwStatus = ERROR_OUTOFMEMORY;
  134. break;
  135. }
  136. }
  137. if ( STATUS_SUCCESS == dwStatus )
  138. {
  139. *ppInterfaceInfo = (PIP_INTERFACE_INFO) pBuffer;
  140. }
  141. TraceMsg(TF_ALWAYS, "GetInterfaceInformation = %lx", dwStatus );
  142. return dwStatus;
  143. }
  144. HRESULT EnableDhcpByGuid( LPOLESTR szwGuid )
  145. //+---------------------------------------------------------------------------
  146. //
  147. // Function: EnableDhcp
  148. //
  149. // Purpose:
  150. //
  151. // Arguments:
  152. //
  153. // Returns: S_OK on success, otherwise an error code
  154. //
  155. // Notes:
  156. //
  157. {
  158. HRESULT hr = E_INVALIDARG;
  159. ASSERT( szwGuid );
  160. if ( szwGuid )
  161. {
  162. char* pszName = NULL;
  163. hr = HrWideCharToMultiByte( szwGuid, &pszName );
  164. if ( SUCCEEDED(hr) )
  165. {
  166. hr = HrSetAdapterIpAddress( pszName, TRUE, 0, 0, 0 );
  167. delete [] pszName;
  168. }
  169. else
  170. {
  171. TraceMsg(TF_ALWAYS, "HrWideCharToMultiByte( %s, &pszName ) FAILED!", szwGuid );
  172. }
  173. }
  174. TraceMsg(TF_ALWAYS, "EnableDhcp = %lx", hr );
  175. return hr;
  176. }
  177. HRESULT HrFindAndConfigureIp( LPOLESTR szwGuid, PIP_INTERFACE_INFO pInterfaceInfo, DWORD dwFlags )
  178. //+---------------------------------------------------------------------------
  179. //
  180. // Function: HrFindAndConfigureIp
  181. //
  182. // Purpose:
  183. //
  184. // Arguments:
  185. //
  186. // Returns: S_OK on success, otherwise an error code
  187. //
  188. // Notes:
  189. //
  190. {
  191. ASSERT( szwGuid );
  192. ASSERT( pInterfaceInfo );
  193. HRESULT hr = E_FAIL;
  194. for ( LONG i=0L; i<pInterfaceInfo->NumAdapters; i++ )
  195. {
  196. WCHAR* szwName = (pInterfaceInfo->Adapter)[i].Name;
  197. // The Interface Info device name includes the full device name
  198. // prefix appended to the device guid. To solve this we move
  199. // the WCHAR pointer past the prefix using the length of the
  200. // the guid string from the INetConnection*
  201. szwName += ( wcslen( szwName ) - wcslen( szwGuid ) );
  202. TraceMsg( TF_ALWAYS, " %s", szwName );
  203. if ( wcscmp( szwGuid, szwName ) == 0 )
  204. {
  205. DWORD dwStatus = STATUS_SUCCESS;
  206. if ( HNW_ED_RELEASE & dwFlags )
  207. {
  208. dwStatus = IpReleaseAddress( &((pInterfaceInfo->Adapter)[i]) );
  209. }
  210. if ( ( HNW_ED_RENEW & dwFlags ) &&
  211. ( STATUS_SUCCESS == dwStatus ) )
  212. {
  213. dwStatus = IpRenewAddress( &((pInterfaceInfo->Adapter)[i]) );
  214. }
  215. if ( STATUS_SUCCESS == dwStatus )
  216. {
  217. hr = S_OK;
  218. break;
  219. }
  220. }
  221. }
  222. TraceMsg( TF_ALWAYS, "HrFindAndConfigureIp = %lx", hr );
  223. return hr;
  224. }
  225. HRESULT ConfigureIp( LPOLESTR szwGuid, DWORD dwFlags )
  226. //+---------------------------------------------------------------------------
  227. //
  228. // Function: ConfigureIp
  229. //
  230. // Purpose:
  231. //
  232. // Arguments:
  233. //
  234. // Returns: S_OK on success, otherwise an error code
  235. //
  236. // Notes:
  237. //
  238. {
  239. ASSERT( szwGuid );
  240. PIP_INTERFACE_INFO pInterfaceInfo;
  241. HRESULT hr = E_FAIL;
  242. DWORD dwStatus = GetInterfaceInformation( &pInterfaceInfo );
  243. if ( STATUS_SUCCESS == dwStatus )
  244. {
  245. hr = HrFindAndConfigureIp( szwGuid, pInterfaceInfo, dwFlags );
  246. delete pInterfaceInfo;
  247. }
  248. TraceMsg(TF_ALWAYS, "ConfigureIp = %lx", hr );
  249. return hr;
  250. }
  251. #ifdef __cplusplus
  252. extern "C" {
  253. #endif
  254. HRESULT WINAPI HrEnableDhcp( VOID* pContext, DWORD dwFlags )
  255. //+---------------------------------------------------------------------------
  256. //
  257. // Function: HrEnableDhcpIfLAN
  258. //
  259. // Purpose:
  260. //
  261. // Arguments: INetConnection* pConnection
  262. // DWORD dwFlags
  263. //
  264. // Returns: HRESULT
  265. //
  266. // Author: billi 26/01/01
  267. //
  268. // Notes:
  269. //
  270. {
  271. HRESULT hr = E_INVALIDARG;
  272. INetConnection* pConnection = (INetConnection *)pContext;
  273. ASSERT( pConnection );
  274. if ( NULL != pConnection )
  275. {
  276. NETCON_PROPERTIES* pProps;
  277. hr = pConnection->GetProperties( &pProps );
  278. if ( SUCCEEDED(hr) )
  279. {
  280. ASSERT( pProps );
  281. if ( NCM_LAN == pProps->MediaType )
  282. {
  283. OLECHAR szwGuid[ GUID_LENGTH + 1 ];
  284. if ( StringFromGUID2( pProps->guidId, szwGuid, GUID_LENGTH+1 ) )
  285. {
  286. hr = EnableDhcpByGuid( szwGuid );
  287. if ( SUCCEEDED(hr) && dwFlags )
  288. {
  289. hr = ConfigureIp( szwGuid, dwFlags );
  290. }
  291. }
  292. else
  293. {
  294. hr = E_FAIL;
  295. }
  296. }
  297. NcFreeNetconProperties( pProps );
  298. }
  299. }
  300. TraceMsg(TF_ALWAYS, "HrEnableDhcp = %lx", hr);
  301. return hr;
  302. }
  303. BOOLEAN WINAPI IsAdapterDisconnected(
  304. VOID* pContext
  305. )
  306. //+---------------------------------------------------------------------------
  307. //
  308. // Function: IsAdapterDisconnected
  309. //
  310. // Purpose:
  311. //
  312. // Arguments: VOID* pNA
  313. //
  314. // Returns: HRESULT
  315. //
  316. // Author: billi 11/04/01
  317. //
  318. // Notes:
  319. //
  320. {
  321. INetConnection* pConnection = (INetConnection *)pContext;
  322. BOOLEAN fUnplugged = FALSE;
  323. HRESULT hr;
  324. NETCON_PROPERTIES* pncprops;
  325. ASSERT(pConnection);
  326. if ( pConnection )
  327. {
  328. hr = pConnection->GetProperties(&pncprops);
  329. if (SUCCEEDED(hr))
  330. {
  331. ASSERT(pncprops);
  332. fUnplugged = (NCS_MEDIA_DISCONNECTED == pncprops->Status);
  333. NcFreeNetconProperties(pncprops);
  334. }
  335. }
  336. return fUnplugged;
  337. }
  338. #ifdef __cplusplus
  339. }
  340. #endif