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.

782 lines
29 KiB

  1. // inets.cpp: implementation of the CInetSetup class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "inets.h"
  5. #include <ras.h>
  6. #include <tapi.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <appdefs.h>
  10. #define MAXNAME 200
  11. typedef DWORD (WINAPI *LPFNDLL_RASGETENTRYPROPERTIES)(LPCWSTR, LPCWSTR, LPRASENTRY, LPDWORD, LPBYTE, LPDWORD);
  12. typedef DWORD (WINAPI *LPFNDLL_RASSETENTRYPROPERTIES)(LPCWSTR, LPCWSTR, LPRASENTRY, DWORD , LPBYTE, DWORD );
  13. /////////////////////////////////////////////////////////////////////////
  14. // Registry Values //
  15. /////////////////////////////////////////////////////////////////////////
  16. // For CInetSetup::InetSSetLanConnection
  17. //static const WCHAR cszRegEnumPciKey[] = L"Enum\\PCI";
  18. //static const WCHAR cszRegEnumNetworkTcpKey[] = L"Enum\\Network\\MSTCP";
  19. //static const WCHAR cszRegClassKey[] = L"System\\CurrentControlSet\\Services\\Class";
  20. static const WCHAR cszRegBindings[] = L"Bindings";
  21. //static const WCHAR cszRegEnumDriverKey[] = L"Driver";
  22. static const WCHAR cszRegTcpIp[] = L"MSTCP";
  23. // Global TcpIp Reg Location
  24. static const WCHAR cszRegFixedTcpInfoKey[] = L"System\\CurrentControlSet\\Services\\VxD\\MSTCP";
  25. //
  26. // IP Address
  27. //
  28. static const WCHAR cszRegIPAddress[] = L"IPAddress";
  29. static const WCHAR cszRegIPMask[] = L"IPMask";
  30. //
  31. // WINS
  32. //
  33. static const WCHAR cszRegWINS[] = L"NameServer";
  34. //
  35. // Gateway
  36. //
  37. static const WCHAR cszRegDefaultGateway[] = L"DefaultGateway";
  38. //
  39. // DNS
  40. //
  41. static const WCHAR cszRegDomainName[] = L"Domain";
  42. static const WCHAR cszRegNameServer[] = L"NameServer";
  43. static const WCHAR cszRegHostName[] = L"HostName";
  44. static const WCHAR cszRegEnableDNS[] = L"EnableDNS";
  45. static const WCHAR cszRegSuffixSearchList[] = L"SearchList";
  46. static const WCHAR cszNullIP[] = L"0.0.0.0";
  47. static const WCHAR cszAdapterClass[] = L"Net";
  48. static const WCHAR cszProtocolClass[] = L"NetTrans";
  49. static const WCHAR cszNodeType[] = L"NodeType";
  50. static const WCHAR cszScopeID[] = L"ScopeID";
  51. // Not in use by OOBE, but included in case this becomes
  52. // a seperate module. API does *NOT* work with PPPOA.
  53. DWORD WINAPI InetSSetRasConnection ( RASINFO& RasEntry )
  54. {
  55. DWORD nRetVal = ERROR_SUCCESS;
  56. LPFNDLL_RASSETENTRYPROPERTIES rsep = NULL;
  57. LPFNDLL_RASGETENTRYPROPERTIES rgep = NULL;
  58. LPBYTE lpDeviceBuf = NULL;
  59. DWORD dwDeviceBufSize = 0;
  60. HMODULE hRasApi = LoadLibrary (L"RasApi32.dll");
  61. if (!hRasApi) return GetLastError();
  62. if (!(rsep = (LPFNDLL_RASSETENTRYPROPERTIES) GetProcAddress (hRasApi, "RasSetEntryProperties"))) {
  63. nRetVal = GetLastError();
  64. goto end;
  65. }
  66. if (!(rgep = (LPFNDLL_RASGETENTRYPROPERTIES) GetProcAddress (hRasApi, "RasGetEntryProperties"))) {
  67. nRetVal = GetLastError();
  68. goto end;
  69. }
  70. if ( (nRetVal = RasValidateEntryName ( ((!lstrlen(RasEntry.szPhoneBook)) ? NULL : RasEntry.szPhoneBook), RasEntry.szEntryName )) != ERROR_SUCCESS &&
  71. nRetVal != ERROR_ALREADY_EXISTS ) {
  72. nRetVal = ERROR_INVALID_NAME;
  73. goto end;
  74. }
  75. // we place the RASENTRY structure first. the lpDeviceInfo information is only considered for
  76. // ATM.
  77. if ( (nRetVal = rsep (((!lstrlen(RasEntry.szPhoneBook)) ? NULL : RasEntry.szPhoneBook), RasEntry.szEntryName, &RasEntry.RasEntry, sizeof (RasEntry.RasEntry), NULL, 0)) != ERROR_SUCCESS ) {
  78. goto end;
  79. }
  80. // unless the device is ATM, no further action is necessary.
  81. //if ( lstrcmpi (RasEntry.RasEntry.szDeviceType, RASDT_Atm) )
  82. {
  83. nRetVal = ERROR_SUCCESS;
  84. goto end;
  85. }
  86. if ( RasEntry.dwDeviceInfoSize != sizeof (ATMPBCONFIG) ) {
  87. nRetVal = ERROR_INVALID_PARAMETER;
  88. goto end;
  89. }
  90. if ( (nRetVal = rgep (((!lstrlen(RasEntry.szPhoneBook)) ? NULL : RasEntry.szPhoneBook), RasEntry.szEntryName, &(RasEntry.RasEntry), &(RasEntry.RasEntry.dwSize), NULL, &dwDeviceBufSize)) != ERROR_SUCCESS ) {
  91. goto end;
  92. }
  93. if ( !(lpDeviceBuf = (LPBYTE) malloc (dwDeviceBufSize)) ) {
  94. nRetVal = ERROR_NOT_ENOUGH_MEMORY;
  95. goto end;
  96. }
  97. if ( (nRetVal = rgep (((!lstrlen(RasEntry.szPhoneBook)) ? NULL : RasEntry.szPhoneBook), RasEntry.szEntryName, &(RasEntry.RasEntry), &(RasEntry.RasEntry.dwSize), lpDeviceBuf, &dwDeviceBufSize)) != ERROR_SUCCESS ) {
  98. goto end;
  99. }
  100. // ** BUGBUG: WARNING: THIS IS NOT STABLE CODE: THERE IS NO DOCUMENTATION ON THE CORRECT
  101. // ** -------- USE OF THE RASSETENTRYPROPERTIES FOR THE LPDEVICEINFO BUFFER.
  102. memcpy (lpDeviceBuf+66, &RasEntry.lpDeviceInfo, RasEntry.dwDeviceInfoSize); // HACK!
  103. if ( (nRetVal = rsep (NULL, RasEntry.szEntryName,
  104. &RasEntry.RasEntry, sizeof (RasEntry.RasEntry),
  105. lpDeviceBuf, dwDeviceBufSize)) != ERROR_SUCCESS) {
  106. goto end;
  107. }
  108. end:
  109. free (lpDeviceBuf);
  110. FreeLibrary(hRasApi);
  111. return nRetVal;
  112. }
  113. // this function sets a PPPOE connection. Presently, it merely updates
  114. // the device's registry location with the parameters in the INS file.
  115. // in the future, InetSSetPppoeConnection () will have native support.
  116. DWORD WINAPI InetSSetPppoeConnection ( PPPOEINFO& PppoeInfo )
  117. {
  118. // settings:
  119. // ---------------------------------------------------
  120. // Format: "RegKey=RegVal" e.g. "Pvc1=10"
  121. // LPBYTE lpbNdiBuf = PppoeInfo.PppoeModule.lpbRegNdiParamBuf;
  122. LPWSTR pwchSetBuf = (LPWSTR)PppoeInfo.PppoeModule.lpbRegSettingsBuf;
  123. LPWSTR eq = 0;
  124. DWORD cwchValue = 0;
  125. HKEY hkeyAdapterClass = NULL;
  126. // BUGBUG: error checking is ignored. BUG-BUG
  127. DWORD nRetVal = 0;
  128. if ( (nRetVal = InetSGetAdapterKey ( cszAdapterClass, PppoeInfo.TcpIpInfo.szPnPId, INETS_ADAPTER_HARDWAREID, DIREG_DRV, hkeyAdapterClass )) != ERROR_SUCCESS )
  129. {
  130. return nRetVal;
  131. }
  132. while ( *pwchSetBuf )
  133. {
  134. if ( !(eq = wcschr ( pwchSetBuf, L'=' )) )
  135. {
  136. return ERROR_INVALID_PARAMETER;
  137. }
  138. // we also disallow the following: "Vci="
  139. if ( !(*(eq+1)) )
  140. {
  141. return ERROR_INVALID_PARAMETER;
  142. }
  143. // flush out the '=' so that we have two token strings.
  144. // we simply move each string directly into the registry.
  145. *eq = L'\0';
  146. cwchValue = lstrlen(eq + 1) + 1; // account for trailing 0
  147. if ( RegSetValueEx ( hkeyAdapterClass, pwchSetBuf, 0, REG_SZ, (LPBYTE)(eq + 1), cwchValue * sizeof(WCHAR)) != ERROR_SUCCESS )
  148. {
  149. *eq = L'=';
  150. return E_FAIL;
  151. }
  152. // restore the '=' and move to the next pair "name=value"
  153. *eq = L'=';
  154. pwchSetBuf = eq + 1 + cwchValue; // include '='
  155. }
  156. if ( InetSSetLanConnection ( PppoeInfo.TcpIpInfo ) != ERROR_SUCCESS )
  157. {
  158. return E_FAIL;
  159. }
  160. return ERROR_SUCCESS;
  161. }
  162. DWORD WINAPI InetSSetRfc1483Connection ( RFC1483INFO &Rfc1483Info )
  163. {
  164. // settings:
  165. // ---------------------------------------------------
  166. // Format: "RegKey=RegVal" e.g. "Pvc1=10"
  167. // LPBYTE lpbNdiBuf = Rfc1483Info.Rfc1483Module.lpbRegNdiParamBuf;
  168. // BUGBUG: What does lpbSetBuf contain??
  169. LPBYTE lpbSetBuf = Rfc1483Info.Rfc1483Module.lpbRegSettingsBuf;
  170. WCHAR *eq = 0;
  171. DWORD_PTR dwNameSize = 0;
  172. DWORD dwValueSize = 0;
  173. HKEY hkeyAdapterClass = NULL;
  174. // BUGBUG: error checking is ignored. BUG-BUG
  175. DWORD nRetVal = 0;
  176. if ( (nRetVal = InetSGetAdapterKey ( cszAdapterClass, Rfc1483Info.TcpIpInfo.szPnPId, INETS_ADAPTER_HARDWAREID, DIREG_DRV, hkeyAdapterClass )) != ERROR_SUCCESS )
  177. {
  178. return nRetVal;
  179. }
  180. while ( *lpbSetBuf )
  181. {
  182. if ( !(eq = wcschr ( (WCHAR*)lpbSetBuf, L'=' )) )
  183. {
  184. return ERROR_INVALID_PARAMETER;
  185. }
  186. // we also disallow the following: "Vci="
  187. if ( !(*(eq+1)) )
  188. {
  189. return ERROR_INVALID_PARAMETER;
  190. }
  191. // flush out the '=' so that we have two token strings.
  192. // we simply move each string directly into the registry.
  193. *eq = L'\0';
  194. dwNameSize = eq-(WCHAR*)lpbSetBuf;
  195. dwValueSize = BYTES_REQUIRED_BY_SZ(eq+1);
  196. if ( RegSetValueEx ( hkeyAdapterClass, (WCHAR*)lpbSetBuf, 0, REG_SZ, (LPBYTE)eq+1, dwValueSize+1) != ERROR_SUCCESS )
  197. {
  198. *eq = L'=';
  199. return E_FAIL;
  200. }
  201. // restore the '=' and move to the next pair "name=value"
  202. *eq = L'=';
  203. lpbSetBuf += dwNameSize+dwValueSize+2; // for '=' and '\0'
  204. }
  205. if ( InetSSetLanConnection ( Rfc1483Info.TcpIpInfo ) != ERROR_SUCCESS )
  206. {
  207. return E_FAIL;
  208. }
  209. return ERROR_SUCCESS;
  210. }
  211. // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-* InetSGetAdapterKey -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* //
  212. //
  213. // Description:
  214. // This function returns a Driver Registry Key for a Device.
  215. //
  216. // Arguments:
  217. // cszDeviceClass - Any Device Class, e.g. "Net", "NetTrans", etc.
  218. // cszDeviceParam - Value that we're using to identify the Device.
  219. // dwEnumType - Can be either INETS_ADAPTER_HARDWAREID or
  220. // INETS_ADAPTER_INSTANCEID.
  221. // dwRequiredKeyType - This corresponds to KeyType in SetupDiOpenDevRegKey
  222. // in SetupAPI.
  223. //
  224. // hkeyDevKey - Registry Key Handle provided by the caller.
  225. //
  226. // Return Values:
  227. // ERROR_SUCCESS - Function returns successfully.
  228. // Other - use GetLastError(). Note: hkeyDevKey = INVALID_HANDLE_VALUE in this case.
  229. //
  230. // Remarks:
  231. // Use this function to browse the Device Manager for Network Devices and Protocol Drivers.
  232. //
  233. DWORD WINAPI InetSGetAdapterKey ( LPCWSTR cszDeviceClass, LPCWSTR cszDeviceParam, DWORD dwEnumType, DWORD dwRequiredKeyType, HKEY &hkeyDevKey ) {
  234. // initialization of parameter
  235. hkeyDevKey = (HKEY) INVALID_HANDLE_VALUE;
  236. DWORD nRetVal = ERROR_SUCCESS;
  237. // We find out the network adapter's TCP/IP Binding first.
  238. HINSTANCE hSetupLib = LoadLibrary (L"SetupApi.Dll");
  239. if (!hSetupLib)
  240. {
  241. return GetLastError();
  242. }
  243. // Get procedures we need from the DLL.
  244. LPFNDLL_SETUPDICLASSGUIDSFROMNAME lpfndll_SetupDiClassGuidsFromName = NULL;
  245. LPFNDLL_SETUPDIGETCLASSDEVS lpfndll_SetupDiGetClassDevs = NULL;
  246. LPFNDLL_SETUPDIENUMDEVICEINFO lpfndll_SetupDiEnumDeviceInfo = NULL;
  247. LPFNDLL_SETUPDIGETDEVICEREGISTRYPROPERTY lpfndll_SetupDiGetDeviceRegistryProperty = NULL;
  248. LPFNDLL_SETUPDIOPENDEVREGKEY lpfndll_SetupDiOpenDevRegKey = NULL;
  249. LPFNDLL_SETUPDIGETDEVICEINSTANCEID lpfndll_SetupDiGetDeviceInstanceId = NULL;
  250. if ( !(lpfndll_SetupDiClassGuidsFromName = (LPFNDLL_SETUPDICLASSGUIDSFROMNAME) GetProcAddress ( hSetupLib, cszSetupDiClassGuidsFromName )) )
  251. {
  252. nRetVal = GetLastError();
  253. FreeLibrary ( hSetupLib );
  254. return nRetVal;
  255. }
  256. if ( !(lpfndll_SetupDiGetClassDevs = (LPFNDLL_SETUPDIGETCLASSDEVS) GetProcAddress ( hSetupLib, cszSetupDiGetClassDevs )) )
  257. {
  258. nRetVal = GetLastError();
  259. FreeLibrary ( hSetupLib );
  260. return nRetVal;
  261. }
  262. if ( !(lpfndll_SetupDiEnumDeviceInfo = (LPFNDLL_SETUPDIENUMDEVICEINFO) GetProcAddress ( hSetupLib, "SetupDiEnumDeviceInfo" )) )
  263. {
  264. nRetVal = GetLastError();
  265. FreeLibrary ( hSetupLib );
  266. return nRetVal;
  267. }
  268. if ( !(lpfndll_SetupDiGetDeviceRegistryProperty = (LPFNDLL_SETUPDIGETDEVICEREGISTRYPROPERTY) GetProcAddress ( hSetupLib, cszSetupDiGetDeviceRegistryProperty )) )
  269. {
  270. nRetVal = GetLastError();
  271. FreeLibrary ( hSetupLib );
  272. return nRetVal;
  273. }
  274. if ( !(lpfndll_SetupDiOpenDevRegKey = (LPFNDLL_SETUPDIOPENDEVREGKEY) GetProcAddress ( hSetupLib, "SetupDiOpenDevRegKey" )) )
  275. {
  276. nRetVal = GetLastError();
  277. FreeLibrary ( hSetupLib );
  278. return nRetVal;
  279. }
  280. if ( !(lpfndll_SetupDiGetDeviceInstanceId = (LPFNDLL_SETUPDIGETDEVICEINSTANCEID) GetProcAddress ( hSetupLib, cszSetupDiGetDeviceInstanceId ) ) )
  281. {
  282. nRetVal = GetLastError();
  283. FreeLibrary ( hSetupLib );
  284. return nRetVal;
  285. }
  286. // fantastic. we have the functions. now, on to business.
  287. // Get Class Guid.
  288. BOOLEAN bRet = FALSE;
  289. DWORD dwArraySize = 0;
  290. LPGUID lpguidArray = NULL;
  291. bRet = lpfndll_SetupDiClassGuidsFromName ( cszDeviceClass, NULL, NULL, &dwArraySize );
  292. // We depend on SetupDiClassGuidsFromName() to provide us with the Guid, and we need to
  293. // allocate space to accomodate the Guid. If this cannot be done, we are crippled!
  294. if ( !dwArraySize )
  295. {
  296. FreeLibrary ( hSetupLib );
  297. return ERROR_INVALID_DATA;
  298. }
  299. if ( !(lpguidArray = (LPGUID) malloc (dwArraySize*sizeof(GUID))) )
  300. {
  301. FreeLibrary ( hSetupLib );
  302. return ERROR_NOT_ENOUGH_MEMORY;
  303. }
  304. if ( !(bRet = (lpfndll_SetupDiClassGuidsFromName ( cszDeviceClass, lpguidArray, dwArraySize, &dwArraySize )) ) )
  305. {
  306. FreeLibrary ( hSetupLib );
  307. free ( lpguidArray );
  308. return ERROR_INVALID_FUNCTION;
  309. }
  310. // we retrieve the list of devices.
  311. HDEVINFO hdevNetDeviceList = lpfndll_SetupDiGetClassDevs ( lpguidArray, NULL, NULL, DIGCF_PRESENT );
  312. if ( !hdevNetDeviceList )
  313. {
  314. FreeLibrary ( hSetupLib );
  315. free ( lpguidArray );
  316. return ERROR_INVALID_FUNCTION;
  317. }
  318. free ( lpguidArray ); // where shall we do this garbage collection ?
  319. // we will now enumerate through the list of Net devices.
  320. SP_DEVINFO_DATA DevInfoStruct;
  321. memset ( &DevInfoStruct, 0, sizeof (DevInfoStruct) );
  322. DevInfoStruct.cbSize = sizeof (DevInfoStruct);
  323. int i = 0;
  324. LPBYTE lpbHardwareIdBuf = NULL;
  325. DWORD dwHardwareIdBufSize = 0;
  326. DWORD dwRequiredSize = 0;
  327. BOOL bFound = FALSE;
  328. const DWORD cdwIncrement = 500; // BUGBUG: What's magic about 500??
  329. if ( !(lpbHardwareIdBuf = (LPBYTE) malloc (500)) )
  330. {
  331. FreeLibrary ( hSetupLib );
  332. return ERROR_NOT_ENOUGH_MEMORY;
  333. }
  334. dwHardwareIdBufSize = 500;
  335. while ( bRet = ( lpfndll_SetupDiEnumDeviceInfo (hdevNetDeviceList, i, &DevInfoStruct )) )
  336. {
  337. // for each Net device, we will compare its hardware ID to the one
  338. // provided in the parameter.
  339. switch ( dwEnumType )
  340. {
  341. case INETS_ADAPTER_HARDWAREID:
  342. while ( !(bRet = lpfndll_SetupDiGetDeviceRegistryProperty ( hdevNetDeviceList, &DevInfoStruct, SPDRP_HARDWAREID, NULL, lpbHardwareIdBuf, dwHardwareIdBufSize, &dwRequiredSize )) && ((nRetVal = GetLastError()) == ERROR_INSUFFICIENT_BUFFER ))
  343. { // we need to reallocate the buffer size.
  344. if ( !dwRequiredSize ) dwHardwareIdBufSize += cdwIncrement;
  345. else dwHardwareIdBufSize += dwRequiredSize;
  346. if ( !(lpbHardwareIdBuf = (LPBYTE) realloc ( (void*) lpbHardwareIdBuf, dwHardwareIdBufSize )) )
  347. {
  348. // not enough memory!
  349. free (lpbHardwareIdBuf);
  350. FreeLibrary ( hSetupLib );
  351. return ERROR_NOT_ENOUGH_MEMORY;
  352. }
  353. }
  354. break;
  355. case INETS_ADAPTER_INSTANCEID:
  356. while ( !(bRet = lpfndll_SetupDiGetDeviceInstanceId ( hdevNetDeviceList, &DevInfoStruct, (PCWSTR) lpbHardwareIdBuf, dwHardwareIdBufSize, &dwRequiredSize )) && ((nRetVal = GetLastError()) == ERROR_INSUFFICIENT_BUFFER ))
  357. {
  358. // we need to reallocate the buffer size.
  359. if ( !dwRequiredSize ) dwHardwareIdBufSize += cdwIncrement;
  360. else dwHardwareIdBufSize += dwRequiredSize;
  361. if ( !(lpbHardwareIdBuf = (LPBYTE) realloc ( (void*) lpbHardwareIdBuf, dwHardwareIdBufSize )) )
  362. {
  363. // not enough memory!
  364. free (lpbHardwareIdBuf);
  365. FreeLibrary ( hSetupLib );
  366. return ERROR_NOT_ENOUGH_MEMORY;
  367. }
  368. }
  369. break;
  370. default:
  371. free (lpbHardwareIdBuf);
  372. FreeLibrary ( hSetupLib );
  373. return ERROR_INVALID_PARAMETER;
  374. }
  375. if ( bRet )
  376. {
  377. // we should have the hardware ID, at this stage. we compare it with
  378. // the device's plug-and-play ID.
  379. // BUGBUG: Is lpbHardwareIdBuf ANSI or Unicode?
  380. if ( wcsstr( (const WCHAR *)lpbHardwareIdBuf, cszDeviceParam) )
  381. {
  382. // found!
  383. bFound = TRUE;
  384. // we get the device's registry key.
  385. if ( (hkeyDevKey = lpfndll_SetupDiOpenDevRegKey ( hdevNetDeviceList, &DevInfoStruct, DICS_FLAG_GLOBAL, 0, dwRequiredKeyType, KEY_ALL_ACCESS )) == INVALID_HANDLE_VALUE )
  386. {
  387. free (lpbHardwareIdBuf);
  388. FreeLibrary ( hSetupLib );
  389. return ERROR_BADKEY;
  390. }
  391. free (lpbHardwareIdBuf);
  392. FreeLibrary ( hSetupLib );
  393. return ERROR_SUCCESS;
  394. }
  395. }
  396. i++;
  397. }
  398. // the while loop enumerated unsuccessfully.
  399. free (lpbHardwareIdBuf);
  400. FreeLibrary ( hSetupLib );
  401. return ERROR_NOT_FOUND;
  402. }
  403. DWORD WINAPI InetSSetLanConnection ( LANINFO& LANINFO )
  404. {
  405. HKEY hkeyAdapter = NULL;
  406. DWORD nRetVal = 0;
  407. HKEY hkeyGlobalTcp = NULL;
  408. LPBYTE lpbBufPtr = NULL;
  409. DWORD dwValueBufSize = 0;
  410. WCHAR *Token = NULL, *PlaceHolder = NULL;
  411. WCHAR *WINSListPtr = NULL;
  412. // TCP/IP InstanceID ==> Class Key
  413. HKEY hkeyClassTcp = NULL;
  414. HKEY hkeyAdapterBinding = NULL;
  415. __try
  416. {
  417. // PnPId ==> Device Configuration Key
  418. if ( (nRetVal = InetSGetAdapterKey ( cszAdapterClass, LANINFO.szPnPId, INETS_ADAPTER_HARDWAREID, DIREG_DEV, hkeyAdapter)) != ERROR_SUCCESS )
  419. {
  420. __leave;
  421. }
  422. // Open the Bindings subkey to look for TCP/IP Binding.
  423. if ( RegOpenKeyEx ( hkeyAdapter, cszRegBindings, 0, KEY_ALL_ACCESS, &hkeyAdapterBinding ) != ERROR_SUCCESS )
  424. {
  425. nRetVal = GetLastError();
  426. __leave;
  427. }
  428. // Find the TCP/IP binding.
  429. WCHAR szBindingValueName [GEN_MAX_STRING_LENGTH];
  430. DWORD dwBindingValueNameSize = sizeof (szBindingValueName) / sizeof(WCHAR);
  431. int index = 0;
  432. while ( RegEnumValue ( hkeyAdapterBinding, index, szBindingValueName, &dwBindingValueNameSize, 0, 0, 0, 0 ) == ERROR_SUCCESS )
  433. {
  434. if ( !wcsncmp ( szBindingValueName, cszRegTcpIp, sizeof (cszRegTcpIp)-1 ) )
  435. {
  436. // we found a binding!
  437. break;
  438. }
  439. index++;
  440. }
  441. if ( (nRetVal = InetSGetAdapterKey ( cszProtocolClass, szBindingValueName, INETS_ADAPTER_INSTANCEID, DIREG_DRV, hkeyClassTcp )) != ERROR_SUCCESS )
  442. {
  443. nRetVal = GetLastError();
  444. __leave;
  445. }
  446. // Got it. we will now start the update.
  447. //
  448. // IP Address
  449. //
  450. lpbBufPtr = 0;
  451. dwValueBufSize = 0;
  452. if ( LANINFO.TcpIpInfo.EnableIP )
  453. {
  454. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szIPAddress;
  455. dwValueBufSize = BYTES_REQUIRED_BY_SZ(LANINFO.TcpIpInfo.szIPAddress);
  456. if (RegSetValueEx ( hkeyClassTcp, cszRegIPAddress, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  457. {
  458. // close handles also!
  459. nRetVal = E_FAIL;
  460. __leave;
  461. }
  462. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szIPMask;
  463. dwValueBufSize = BYTES_REQUIRED_BY_SZ(LANINFO.TcpIpInfo.szIPMask);
  464. if (RegSetValueEx ( hkeyClassTcp, cszRegIPMask, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  465. {
  466. // close handles also!
  467. nRetVal = E_FAIL;
  468. __leave;
  469. }
  470. }
  471. else
  472. {
  473. lpbBufPtr = (LPBYTE) cszNullIP;
  474. dwValueBufSize = BYTES_REQUIRED_BY_SZ(cszNullIP);
  475. if (RegSetValueEx ( hkeyClassTcp, cszRegIPAddress, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  476. {
  477. // close handles also!
  478. nRetVal = E_FAIL;
  479. __leave;
  480. }
  481. if (RegSetValueEx ( hkeyClassTcp, cszRegIPMask, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  482. {
  483. // close handles also!
  484. nRetVal = E_FAIL;
  485. __leave;
  486. }
  487. }
  488. //
  489. // WINS
  490. //
  491. lpbBufPtr = 0;
  492. dwValueBufSize = 0;
  493. index = 1;
  494. WCHAR szWINSEntry [GEN_MAX_STRING_LENGTH];
  495. WCHAR szWINSListCopy [GEN_MAX_STRING_LENGTH];
  496. WINSListPtr = szWINSListCopy;
  497. // BUGBUG: Is LANINFO.TcpIpInfo.szWINSList ANSI or Unicode?
  498. lstrcpy (WINSListPtr, LANINFO.TcpIpInfo.szWINSList);
  499. wsprintf (szWINSEntry, L"%s%d", cszRegWINS, index);
  500. PlaceHolder = szWINSEntry+lstrlen(cszRegWINS);
  501. if ( LANINFO.TcpIpInfo.EnableWINS )
  502. {
  503. while ( Token = wcstok ((index > 1) ? NULL : WINSListPtr, L", " )) { // WARNING. wcstok uses static data! Also the whitespace in ", " is necessary!
  504. if (!Token)
  505. {
  506. nRetVal = E_FAIL;
  507. __leave;
  508. }
  509. lpbBufPtr = (LPBYTE) Token;
  510. dwValueBufSize = BYTES_REQUIRED_BY_SZ(Token);
  511. if (RegSetValueEx ( hkeyClassTcp, szWINSEntry, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  512. {
  513. // close handles also!
  514. nRetVal = E_FAIL;
  515. __leave;
  516. }
  517. wsprintf (PlaceHolder, L"%d", ++index);
  518. }
  519. if (RegSetValueEx ( hkeyClassTcp, cszNodeType, 0, REG_SZ, (LPBYTE) L"8", sizeof (L"8") ) != ERROR_SUCCESS)
  520. {
  521. nRetVal = E_FAIL;
  522. __leave;
  523. }
  524. }
  525. else
  526. {
  527. // TODO: Remove all instances of NameServerX <== IMPORTANT
  528. index = 0;
  529. WCHAR szEnumValueBuffer[GEN_MAX_STRING_LENGTH];
  530. DWORD dwEnumValueBufferSize;
  531. while ( RegEnumValue ( hkeyClassTcp, index, szEnumValueBuffer, &(dwEnumValueBufferSize=sizeof(szEnumValueBuffer)/sizeof(WCHAR)), 0, 0, 0, 0 ) != ERROR_NO_MORE_ITEMS )
  532. {
  533. if ( !wcsncmp (szEnumValueBuffer, cszRegWINS, sizeof (cszRegWINS)-1) )
  534. {
  535. if ( RegDeleteValue ( hkeyClassTcp, szEnumValueBuffer ) != ERROR_SUCCESS )
  536. {
  537. nRetVal = E_FAIL;
  538. __leave;
  539. }
  540. continue;
  541. }
  542. index++;
  543. }
  544. if (RegSetValueEx ( hkeyClassTcp, cszNodeType, 0, REG_SZ, (LPBYTE) L"1", sizeof (L"1") ) != ERROR_SUCCESS)
  545. {
  546. nRetVal = E_FAIL;
  547. __leave;
  548. }
  549. }
  550. //
  551. // Default Gateway
  552. //
  553. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szDefaultGatewayList;
  554. dwValueBufSize = BYTES_REQUIRED_BY_SZ (LANINFO.TcpIpInfo.szDefaultGatewayList);
  555. if (RegSetValueEx ( hkeyClassTcp, cszRegDefaultGateway, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  556. {
  557. // close handles also!
  558. nRetVal = E_FAIL;
  559. __leave;
  560. }
  561. // Step 4: Update global TCPIP entries (DNS)
  562. if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE, cszRegFixedTcpInfoKey, 0, KEY_ALL_ACCESS, &hkeyGlobalTcp) != ERROR_SUCCESS )
  563. {
  564. // close keys
  565. nRetVal = E_FAIL;
  566. __leave;
  567. }
  568. if ( LANINFO.TcpIpInfo.EnableDNS )
  569. {
  570. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szHostName;
  571. dwValueBufSize = BYTES_REQUIRED_BY_SZ (LANINFO.TcpIpInfo.szHostName);
  572. if (RegSetValueEx ( hkeyGlobalTcp, cszRegHostName, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  573. {
  574. // close handles also!
  575. nRetVal = E_FAIL;
  576. __leave;
  577. }
  578. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szDomainName;
  579. dwValueBufSize = BYTES_REQUIRED_BY_SZ (LANINFO.TcpIpInfo.szDomainName);
  580. if (RegSetValueEx ( hkeyGlobalTcp, cszRegDomainName, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  581. {
  582. // close handles also!
  583. nRetVal = E_FAIL;
  584. __leave;
  585. }
  586. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szDNSList;
  587. dwValueBufSize = BYTES_REQUIRED_BY_SZ (LANINFO.TcpIpInfo.szDNSList);
  588. if (RegSetValueEx ( hkeyGlobalTcp, cszRegNameServer, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  589. {
  590. // close handles also!
  591. nRetVal = E_FAIL;
  592. __leave;
  593. }
  594. lpbBufPtr = (LPBYTE) LANINFO.TcpIpInfo.szSuffixSearchList;
  595. dwValueBufSize = BYTES_REQUIRED_BY_SZ (LANINFO.TcpIpInfo.szSuffixSearchList);
  596. if (RegSetValueEx ( hkeyGlobalTcp, cszRegSuffixSearchList, 0, REG_SZ, lpbBufPtr, dwValueBufSize) != ERROR_SUCCESS)
  597. {
  598. // close handles also!
  599. nRetVal = E_FAIL;
  600. __leave;
  601. }
  602. if (RegSetValueEx ( hkeyGlobalTcp, cszRegEnableDNS, 0, REG_SZ, (LPBYTE)L"1", sizeof(L"1")) != ERROR_SUCCESS)
  603. {
  604. // close handles also!
  605. nRetVal = E_FAIL;
  606. __leave;
  607. }
  608. }
  609. else
  610. {
  611. if (RegSetValueEx ( hkeyGlobalTcp, cszRegEnableDNS, 0, REG_SZ, (LPBYTE)L"0", sizeof(L"0")) != ERROR_SUCCESS)
  612. {
  613. // close handles also!
  614. nRetVal = E_FAIL;
  615. __leave;
  616. }
  617. }
  618. WCHAR szScopeID[GEN_MAX_STRING_LENGTH];
  619. if ( LANINFO.TcpIpInfo.EnableWINS )
  620. {
  621. if (LANINFO.TcpIpInfo.uiScopeID == (UINT)~0x0) // this line implies that no ScopeID is given.
  622. {
  623. if ( RegSetValueEx ( hkeyGlobalTcp, cszScopeID, 0, REG_SZ, (LPBYTE)L"", sizeof(L"") ) )
  624. {
  625. nRetVal = E_FAIL;
  626. __leave;
  627. }
  628. }
  629. else if (RegSetValueEx(hkeyGlobalTcp,
  630. cszScopeID,
  631. 0,
  632. REG_SZ,
  633. (LPBYTE)_itow( LANINFO.TcpIpInfo.uiScopeID, szScopeID, 10 ),
  634. BYTES_REQUIRED_BY_SZ(szScopeID)
  635. ) != ERROR_SUCCESS )
  636. {
  637. nRetVal = E_FAIL;
  638. __leave;
  639. }
  640. }
  641. else
  642. {
  643. if ( RegDeleteValue ( hkeyGlobalTcp, cszScopeID ) != ERROR_SUCCESS )
  644. {
  645. nRetVal = E_FAIL;
  646. __leave;
  647. }
  648. }
  649. }
  650. // end.
  651. __finally
  652. {
  653. if ( hkeyAdapter )
  654. {
  655. RegCloseKey ( hkeyAdapter );
  656. }
  657. if ( hkeyGlobalTcp )
  658. {
  659. RegCloseKey ( hkeyGlobalTcp );
  660. }
  661. if ( hkeyAdapterBinding )
  662. {
  663. RegCloseKey ( hkeyAdapterBinding );
  664. }
  665. if ( hkeyClassTcp )
  666. {
  667. RegCloseKey ( hkeyClassTcp );
  668. }
  669. }
  670. return nRetVal;
  671. }
  672. /*
  673. int main() {
  674. CInetSetup inetSetup;
  675. const WCHAR cszINS[] = L"C:\\test.ins";
  676. LANINFO LanInfo;
  677. RASINFO RasInfo;
  678. memset ( &RasInfo, 0, sizeof(RASINFO) );
  679. memset ( &LanInfo, 0, sizeof(LANINFO) );
  680. RasInfo.dwDeviceInfoSize = sizeof(ATMPBCONFIG);
  681. RasInfo.lpDeviceInfo = (LPBYTE) malloc (sizeof (ATMPBCONFIG));
  682. RasInfo.RasEntry.dwSize = sizeof (RASENTRY);
  683. RasInfo.RasEntry.dwfNetProtocols = RASNP_Ip;
  684. RasInfo.RasEntry.dwFramingProtocol = RASFP_Ppp;
  685. lstrcpy ( RasInfo.RasEntry.szDeviceType, RASDT_Modem );
  686. lstrcpy ( RasInfo.RasEntry.szDeviceName, L"Standard 56000 bps V90 Modem" );
  687. lstrcpy ( RasInfo.RasEntry.szLocalPhoneNumber, L"5551212" );
  688. lstrcpy ( RasInfo.szEntryName, L"Test1" );
  689. lstrcpy ( RasInfo.szPhoneBook, L"" );
  690. inetSetup.InetSImportLanConnection ( LanInfo, cszINS );
  691. inetSetup.InetSSetLanConnection ( LanInfo );
  692. inetSetup.InetSSetRasConnection ( RasInfo );
  693. free ( RasInfo.lpDeviceInfo );
  694. return 0;
  695. }
  696. */