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.

1145 lines
27 KiB

  1. /*
  2. Copyright (c) 1998, Microsoft Corporation, all rights reserved
  3. Description:
  4. HBO: host byte order. Used by DNS, DHCP (except in DhcpNotifyConfigChange).
  5. NBO: network byte order. Used by IPCP, inet_ntoa, inet_addr, the stack
  6. (SetProxyArpEntryToStack, IPRouteEntry, etc).
  7. */
  8. #include "helper_.h"
  9. #include "reghelp.h" // for RegHelpGuidFromString
  10. /*
  11. Returns:
  12. VOID
  13. Description:
  14. */
  15. VOID
  16. TraceHlp(
  17. IN CHAR* Format,
  18. ...
  19. )
  20. {
  21. va_list arglist;
  22. RTASSERT(NULL != Format);
  23. va_start(arglist, Format);
  24. TraceVprintfEx(HelperTraceId,
  25. 0x00010000 | TRACE_USE_MASK | TRACE_USE_MSEC,
  26. Format,
  27. arglist);
  28. va_end(arglist);
  29. }
  30. /*
  31. Returns:
  32. Notes:
  33. */
  34. DWORD
  35. HelperInitialize(
  36. OUT HINSTANCE* phInstanceDhcpDll
  37. )
  38. {
  39. DWORD dwErr = NO_ERROR;
  40. while (InterlockedIncrement(&HelperLock) > 1)
  41. {
  42. InterlockedDecrement(&HelperLock);
  43. if (!HelperInitialized)
  44. {
  45. Sleep(1000);
  46. }
  47. else
  48. {
  49. *phInstanceDhcpDll = HelperDhcpDll;
  50. goto LDone;
  51. }
  52. }
  53. ZeroMemory(&HelperRegVal, sizeof(HelperRegVal));
  54. HelperTraceId = TraceRegister("RASIPHLP");
  55. TraceHlp("HelperInitialize");
  56. HelperDhcpDll = LoadLibrary("dhcpcsvc.dll");
  57. if (NULL == HelperDhcpDll)
  58. {
  59. dwErr = GetLastError();
  60. TraceHlp("LoadLibrary(dhcpcsvc.dll) failed and returned %d",
  61. dwErr);
  62. goto LDone;
  63. }
  64. HelperIpHlpDll = LoadLibrary("iphlpapi.dll");
  65. if (NULL == HelperIpHlpDll)
  66. {
  67. dwErr = GetLastError();
  68. TraceHlp("LoadLibrary(iphlpapi.dll) failed and returned %d",
  69. dwErr);
  70. goto LDone;
  71. }
  72. HelperIpBootpDll = LoadLibrary("ipbootp.dll");
  73. if (NULL == HelperIpBootpDll)
  74. {
  75. TraceHlp("LoadLibrary(ipbootp.dll) failed and returned %d",
  76. GetLastError());
  77. }
  78. dwErr = helperGetAddressOfProcs();
  79. if (NO_ERROR != dwErr)
  80. {
  81. goto LDone;
  82. }
  83. /*
  84. This is done to send an IRP_MJ_CREATE to the driver. WanArp starts
  85. "working" (initializes itself etc) only when a component opens it. When the
  86. router is running, this is done by the router manager, but in the ras
  87. client case we need to force WanArp to start.
  88. */
  89. HelperWanArpHandle = CreateFile(
  90. WANARP_DOS_NAME_T,
  91. GENERIC_READ | GENERIC_WRITE,
  92. FILE_SHARE_READ | FILE_SHARE_WRITE,
  93. NULL,
  94. OPEN_EXISTING,
  95. FILE_FLAG_OVERLAPPED,
  96. NULL);
  97. if (INVALID_HANDLE_VALUE == HelperWanArpHandle)
  98. {
  99. dwErr = GetLastError();
  100. TraceHlp("CreateFile(%s) failed and returned %d",
  101. WANARP_DOS_NAME_A, dwErr);
  102. goto LDone;
  103. }
  104. // This is done last. If something fails above, we don't have to
  105. // DeleteCriticalSection.
  106. InitializeCriticalSection(&RasDhcpCriticalSection);
  107. InitializeCriticalSection(&RasStatCriticalSection);
  108. InitializeCriticalSection(&RasSrvrCriticalSection);
  109. InitializeCriticalSection(&RasTimrCriticalSection);
  110. HelperChangeNotification();
  111. *phInstanceDhcpDll = HelperDhcpDll;
  112. HelperInitialized = TRUE;
  113. LDone:
  114. if (NO_ERROR != dwErr)
  115. {
  116. if ((DWORD)-1 != HelperTraceId)
  117. {
  118. TraceDeregister(HelperTraceId);
  119. HelperTraceId = (DWORD)-1;
  120. }
  121. if (INVALID_HANDLE_VALUE != HelperWanArpHandle)
  122. {
  123. CloseHandle(HelperWanArpHandle);
  124. HelperWanArpHandle = INVALID_HANDLE_VALUE;
  125. }
  126. if (NULL != HelperDhcpDll)
  127. {
  128. FreeLibrary(HelperDhcpDll);
  129. HelperDhcpDll = NULL;
  130. }
  131. if (NULL != HelperIpHlpDll)
  132. {
  133. FreeLibrary(HelperIpHlpDll);
  134. HelperIpHlpDll = NULL;
  135. }
  136. if (NULL != HelperIpBootpDll)
  137. {
  138. FreeLibrary(HelperIpBootpDll);
  139. HelperIpBootpDll = NULL;
  140. }
  141. PDhcpNotifyConfigChange = NULL;
  142. PDhcpLeaseIpAddress = NULL;
  143. PDhcpRenewIpAddressLease = NULL;
  144. PDhcpReleaseIpAddressLease = NULL;
  145. PAllocateAndGetIpAddrTableFromStack = NULL;
  146. PSetProxyArpEntryToStack = NULL;
  147. PSetIpForwardEntryToStack = NULL;
  148. PSetIpForwardEntry = NULL;
  149. PDeleteIpForwardEntry = NULL;
  150. PNhpAllocateAndGetInterfaceInfoFromStack = NULL;
  151. PAllocateAndGetIpForwardTableFromStack = NULL;
  152. PGetAdaptersInfo = NULL;
  153. PGetPerAdapterInfo = NULL;
  154. PEnableDhcpInformServer = NULL;
  155. PDisableDhcpInformServer = NULL;
  156. ZeroMemory(&HelperRegVal, sizeof(HelperRegVal));
  157. InterlockedDecrement(&HelperLock);
  158. }
  159. return(dwErr);
  160. }
  161. /*
  162. Returns:
  163. VOID
  164. Notes:
  165. */
  166. VOID
  167. HelperUninitialize(
  168. VOID
  169. )
  170. {
  171. TraceHlp("HelperUninitialize");
  172. if ((DWORD)-1 != HelperTraceId)
  173. {
  174. TraceDeregister(HelperTraceId);
  175. HelperTraceId = (DWORD)-1;
  176. }
  177. if (INVALID_HANDLE_VALUE != HelperWanArpHandle)
  178. {
  179. CloseHandle(HelperWanArpHandle);
  180. HelperWanArpHandle = INVALID_HANDLE_VALUE;
  181. }
  182. if (NULL != HelperDhcpDll)
  183. {
  184. FreeLibrary(HelperDhcpDll);
  185. HelperDhcpDll = NULL;
  186. }
  187. if (NULL != HelperIpHlpDll)
  188. {
  189. FreeLibrary(HelperIpHlpDll);
  190. HelperIpHlpDll = NULL;
  191. }
  192. if (NULL != HelperIpBootpDll)
  193. {
  194. FreeLibrary(HelperIpBootpDll);
  195. HelperIpBootpDll = NULL;
  196. }
  197. RasStatFreeAddrPool(HelperRegVal.pAddrPool);
  198. HelperRegVal.pAddrPool = NULL;
  199. PDhcpNotifyConfigChange = NULL;
  200. PDhcpLeaseIpAddress = NULL;
  201. PDhcpRenewIpAddressLease = NULL;
  202. PDhcpReleaseIpAddressLease = NULL;
  203. PAllocateAndGetIpAddrTableFromStack = NULL;
  204. PSetProxyArpEntryToStack = NULL;
  205. PSetIpForwardEntryToStack = NULL;
  206. PSetIpForwardEntry = NULL;
  207. PDeleteIpForwardEntry = NULL;
  208. PNhpAllocateAndGetInterfaceInfoFromStack = NULL;
  209. PAllocateAndGetIpForwardTableFromStack = NULL;
  210. PGetAdaptersInfo = NULL;
  211. PGetPerAdapterInfo = NULL;
  212. PEnableDhcpInformServer = NULL;
  213. PDisableDhcpInformServer = NULL;
  214. ZeroMemory(&HelperRegVal, sizeof(HelperRegVal));
  215. if (HelperInitialized)
  216. {
  217. DeleteCriticalSection(&RasDhcpCriticalSection);
  218. DeleteCriticalSection(&RasStatCriticalSection);
  219. DeleteCriticalSection(&RasSrvrCriticalSection);
  220. DeleteCriticalSection(&RasTimrCriticalSection);
  221. HelperInitialized = FALSE;
  222. InterlockedDecrement(&HelperLock);
  223. }
  224. }
  225. /*
  226. Returns:
  227. VOID
  228. Description:
  229. */
  230. VOID
  231. HelperChangeNotification(
  232. VOID
  233. )
  234. {
  235. BOOL fUseDhcpAddressingOld;
  236. BOOL fUseDhcpAddressing = TRUE;
  237. ADDR_POOL* pAddrPool = NULL;
  238. BOOL fNICChosen;
  239. GUID guidChosenNIC;
  240. HANDLE h;
  241. DWORD dwNumBytes;
  242. HKEY hKey = NULL;
  243. LONG lErr;
  244. DWORD dwErr;
  245. TraceHlp("HelperChangeNotification");
  246. fUseDhcpAddressingOld = HelperRegVal.fUseDhcpAddressing;
  247. fNICChosen = HelperRegVal.fNICChosen;
  248. CopyMemory(&guidChosenNIC, &(HelperRegVal.guidChosenNIC), sizeof(GUID));
  249. lErr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_RAS_IP_PARAM_A, 0, KEY_READ,
  250. &hKey);
  251. if (ERROR_SUCCESS == lErr)
  252. {
  253. dwNumBytes = sizeof(fUseDhcpAddressing);
  254. lErr = RegQueryValueEx(hKey, REGVAL_USEDHCPADDRESSING_A, NULL, NULL,
  255. (BYTE*)&fUseDhcpAddressing, &dwNumBytes);
  256. if (ERROR_SUCCESS != lErr)
  257. {
  258. fUseDhcpAddressing = TRUE;
  259. }
  260. }
  261. helperReadRegistry();
  262. if (!fUseDhcpAddressing)
  263. {
  264. RasStatCreatePoolList(&pAddrPool);
  265. }
  266. EnterCriticalSection(&RasSrvrCriticalSection);
  267. if (RasSrvrRunning)
  268. {
  269. RasSrvrEnableRouter(HelperRegVal.fEnableRoute);
  270. if ( (fUseDhcpAddressingOld != fUseDhcpAddressing)
  271. || ( !fUseDhcpAddressing
  272. && RasStatAddrPoolsDiffer(HelperRegVal.pAddrPool, pAddrPool))
  273. || ( fUseDhcpAddressing
  274. && ( (fNICChosen != HelperRegVal.fNICChosen)
  275. || ( fNICChosen
  276. && (!IsEqualGUID(&guidChosenNIC,
  277. &(HelperRegVal.guidChosenNIC)))))))
  278. {
  279. RasSrvrStop(TRUE /* fParametersChanged */);
  280. HelperRegVal.fUseDhcpAddressing = fUseDhcpAddressing;
  281. RasStatFreeAddrPool(HelperRegVal.pAddrPool);
  282. HelperRegVal.pAddrPool = pAddrPool;
  283. dwErr = RasSrvrStart();
  284. if (NO_ERROR != dwErr)
  285. {
  286. TraceHlp("RasSrvrStart failed and returned %d");
  287. }
  288. }
  289. }
  290. else
  291. {
  292. HelperRegVal.fUseDhcpAddressing = fUseDhcpAddressing;
  293. RasStatFreeAddrPool(HelperRegVal.pAddrPool);
  294. HelperRegVal.pAddrPool = pAddrPool;
  295. }
  296. LeaveCriticalSection(&RasSrvrCriticalSection);
  297. if (NULL != hKey)
  298. {
  299. RegCloseKey(hKey);
  300. }
  301. }
  302. /*
  303. Returns:
  304. Description:
  305. */
  306. DWORD
  307. HelperSetDefaultInterfaceNet(
  308. IN IPADDR nboIpAddrLocal,
  309. IN IPADDR nboIpAddrRemote,
  310. IN BOOL fPrioritize,
  311. IN WCHAR *pszDevice
  312. )
  313. {
  314. DWORD dwErr = NO_ERROR;
  315. GUID DeviceGuid;
  316. TraceHlp("HelperSetDefaultInterfaceNet(IP addr: 0x%x, fPrioritize: %d)",
  317. nboIpAddrLocal, fPrioritize);
  318. if(NULL == pszDevice)
  319. {
  320. dwErr = ERROR_INVALID_PARAMETER;
  321. goto LDone;
  322. }
  323. RegHelpGuidFromString(pszDevice, &DeviceGuid);
  324. // If fPrioritize flag is set, "fix" the metrics so that the packets go on
  325. // the RAS links
  326. if (fPrioritize)
  327. {
  328. dwErr = RasTcpAdjustRouteMetrics(nboIpAddrLocal, TRUE);
  329. if (NO_ERROR != dwErr)
  330. {
  331. goto LDone;
  332. }
  333. }
  334. // Add code to check for the remote network - same as the one of the local
  335. // networks - if so, set the subnet route to be over the RAS adapter -
  336. // making the ras link as the primary adapter
  337. // We add a Default route to make RAS adapter as the default net if
  338. // fPrioritize flag is set.
  339. if (fPrioritize)
  340. {
  341. // RasTcpSetRoutesForNameServers(TRUE);
  342. RasTcpSetRouteEx(
  343. ALL_NETWORKS_ROUTE,
  344. nboIpAddrLocal,
  345. 0,
  346. nboIpAddrLocal,
  347. TRUE,
  348. 1,
  349. TRUE,
  350. &DeviceGuid);
  351. }
  352. else
  353. {
  354. IPADDR nboMask;
  355. nboMask = RasTcpDeriveMask(nboIpAddrLocal);
  356. if (nboMask != 0)
  357. {
  358. RasTcpSetRouteEx(
  359. nboIpAddrLocal & nboMask,
  360. nboIpAddrLocal,
  361. nboMask,
  362. nboIpAddrLocal,
  363. TRUE,
  364. 1,
  365. TRUE,
  366. &DeviceGuid);
  367. }
  368. }
  369. if (0 != nboIpAddrRemote)
  370. {
  371. RasTcpSetRouteEx(
  372. nboIpAddrRemote,
  373. nboIpAddrLocal,
  374. HOST_MASK,
  375. nboIpAddrLocal,
  376. TRUE,
  377. 1,
  378. TRUE,
  379. &DeviceGuid);
  380. }
  381. LDone:
  382. return(dwErr);
  383. }
  384. /*
  385. Returns:
  386. Description:
  387. */
  388. DWORD
  389. HelperResetDefaultInterfaceNet(
  390. IN IPADDR nboIpAddr,
  391. IN BOOL fPrioritize
  392. )
  393. {
  394. DWORD dwErr = NO_ERROR;;
  395. if (fPrioritize)
  396. {
  397. // RasTcpSetRoutesForNameServers(FALSE);
  398. dwErr = RasTcpAdjustRouteMetrics(nboIpAddr, FALSE);
  399. }
  400. return(dwErr);
  401. }
  402. /*
  403. Returns:
  404. Notes:
  405. */
  406. DWORD
  407. HelperSetDefaultInterfaceNetEx(
  408. IPADDR nboIpAddr,
  409. WCHAR* wszDevice,
  410. BOOL fPrioritize,
  411. WCHAR* wszDnsAddress,
  412. WCHAR* wszDns2Address,
  413. WCHAR* wszWinsAddress,
  414. WCHAR* wszWins2Address
  415. )
  416. {
  417. DWORD dwErr = NO_ERROR;
  418. IPADDR nboIpMask = HOST_MASK;
  419. TCPIP_INFO* pTcpipInfo = NULL;
  420. TraceHlp("HelperSetDefaultInterfaceNetEx(IP addr: 0x%x, Device: %ws, "
  421. "fPrioritize: %d, DNS1: %ws, DNS2: %ws, WINS1: %ws, WINS2: %ws",
  422. nboIpAddr, wszDevice, fPrioritize, wszDnsAddress, wszDns2Address,
  423. wszWinsAddress, wszWins2Address);
  424. dwErr = LoadTcpipInfo(&pTcpipInfo, wszDevice, FALSE /* fAdapterOnly */);
  425. if (NO_ERROR != dwErr)
  426. {
  427. goto LDone;
  428. }
  429. AbcdWszFromIpAddress(nboIpAddr, pTcpipInfo->wszIPAddress);
  430. AbcdWszFromIpAddress(nboIpMask, pTcpipInfo->wszSubnetMask);
  431. // Since we are adding the addresses to the head of the list, we need to add
  432. // the backup DNS and WINS addresses before the primary addresses, so the
  433. // primary ones will be at the head of the list when we're done.
  434. if (wszDns2Address != NULL)
  435. {
  436. dwErr = PrependWszIpAddress(&pTcpipInfo->wszDNSNameServers,
  437. wszDns2Address);
  438. if (NO_ERROR != dwErr)
  439. {
  440. goto LDone;
  441. }
  442. }
  443. if (wszDnsAddress != NULL)
  444. {
  445. dwErr = PrependWszIpAddress(&pTcpipInfo->wszDNSNameServers,
  446. wszDnsAddress);
  447. if (NO_ERROR != dwErr)
  448. {
  449. goto LDone;
  450. }
  451. }
  452. if (wszWins2Address != NULL)
  453. {
  454. dwErr = PrependWszIpAddressToMwsz(&pTcpipInfo->mwszNetBIOSNameServers,
  455. wszWins2Address);
  456. if (NO_ERROR != dwErr)
  457. {
  458. goto LDone;
  459. }
  460. }
  461. if (wszWinsAddress != NULL)
  462. {
  463. dwErr = PrependWszIpAddressToMwsz(&pTcpipInfo->mwszNetBIOSNameServers,
  464. wszWinsAddress);
  465. if (NO_ERROR != dwErr)
  466. {
  467. goto LDone;
  468. }
  469. }
  470. pTcpipInfo->fChanged = TRUE;
  471. dwErr = SaveTcpipInfo(pTcpipInfo);
  472. if (NO_ERROR != dwErr)
  473. {
  474. goto LDone;
  475. }
  476. dwErr = PDhcpNotifyConfigChange(NULL, wszDevice, TRUE, 0, nboIpAddr,
  477. nboIpMask, IgnoreFlag);
  478. if (NO_ERROR != dwErr)
  479. {
  480. TraceHlp("DhcpNotifyConfigChange failed and returned %d", dwErr);
  481. goto LDone;
  482. }
  483. // If fPrioritize flag is set "Fix" the metrics so that the packets go on
  484. // the RAS links
  485. if (fPrioritize)
  486. {
  487. dwErr = RasTcpAdjustRouteMetrics(nboIpAddr, TRUE);
  488. if (NO_ERROR != dwErr)
  489. {
  490. goto LDone;
  491. }
  492. }
  493. // Add code to check for the remote network - same as the one of the local
  494. // networks - if so, set the subnet route to be over the RAS adapter -
  495. // making the RAS link as the primary adapter
  496. // We add a Default route to make RAS adapter as the default net if
  497. // fPrioritize flag is set.
  498. if (fPrioritize)
  499. {
  500. // RasTcpSetRoutesForNameServers(TRUE);
  501. RasTcpSetRoute(ALL_NETWORKS_ROUTE,
  502. nboIpAddr,
  503. 0,
  504. nboIpAddr,
  505. TRUE,
  506. 1,
  507. TRUE);
  508. }
  509. else
  510. {
  511. IPADDR nboMask;
  512. nboMask = RasTcpDeriveMask(nboIpAddr);
  513. if (nboMask != 0)
  514. {
  515. RasTcpSetRoute(
  516. nboIpAddr & nboMask,
  517. nboIpAddr,
  518. nboMask,
  519. nboIpAddr,
  520. TRUE,
  521. 1,
  522. TRUE);
  523. }
  524. }
  525. LDone:
  526. if (pTcpipInfo != NULL)
  527. {
  528. FreeTcpipInfo(&pTcpipInfo);
  529. }
  530. return(dwErr);
  531. }
  532. /*
  533. Returns:
  534. Notes:
  535. */
  536. DWORD
  537. HelperResetDefaultInterfaceNetEx(
  538. IPADDR nboIpAddr,
  539. WCHAR* wszDevice,
  540. BOOL fPrioritize,
  541. WCHAR* wszDnsAddress,
  542. WCHAR* wszDns2Address,
  543. WCHAR* wszWinsAddress,
  544. WCHAR* wszWins2Address
  545. )
  546. {
  547. DWORD dwErr = NO_ERROR;
  548. TCPIP_INFO* pTcpipInfo = NULL;
  549. TraceHlp("HelperResetDefaultInterfaceNetEx(0x%x)", nboIpAddr);
  550. if (fPrioritize)
  551. {
  552. // RasTcpSetRoutesForNameServers(FALSE);
  553. RasTcpAdjustRouteMetrics(nboIpAddr, FALSE);
  554. }
  555. dwErr = LoadTcpipInfo(&pTcpipInfo, wszDevice, TRUE /* fAdapterOnly */);
  556. if (NO_ERROR != dwErr)
  557. {
  558. goto LDone;
  559. }
  560. pTcpipInfo->fChanged = TRUE;
  561. dwErr = SaveTcpipInfo(pTcpipInfo);
  562. if (NO_ERROR != dwErr)
  563. {
  564. goto LDone;
  565. }
  566. dwErr = PDhcpNotifyConfigChange(NULL, wszDevice, TRUE, 0, 0, 0, IgnoreFlag);
  567. if (NO_ERROR != dwErr)
  568. {
  569. TraceHlp("DhcpNotifyConfigChange failed and returned %d", dwErr);
  570. dwErr = NO_ERROR;
  571. }
  572. LDone:
  573. if (pTcpipInfo != NULL)
  574. {
  575. FreeTcpipInfo(&pTcpipInfo);
  576. }
  577. return(dwErr);
  578. }
  579. /*
  580. Returns:
  581. Description:
  582. */
  583. DWORD
  584. helperGetAddressOfProcs(
  585. VOID
  586. )
  587. {
  588. DWORD dwErr = NO_ERROR;
  589. PDhcpNotifyConfigChange = (DHCPNOTIFYCONFIGCHANGE)
  590. GetProcAddress(HelperDhcpDll, "DhcpNotifyConfigChange");
  591. if (NULL == PDhcpNotifyConfigChange)
  592. {
  593. dwErr = GetLastError();
  594. TraceHlp("GetProcAddress(DhcpNotifyConfigChange) failed and returned "
  595. "%d", dwErr);
  596. goto LDone;
  597. }
  598. PDhcpLeaseIpAddress = (DHCPLEASEIPADDRESS)
  599. GetProcAddress(HelperDhcpDll, "DhcpLeaseIpAddress");
  600. if (NULL == PDhcpLeaseIpAddress)
  601. {
  602. dwErr = GetLastError();
  603. TraceHlp("GetProcAddress(DhcpLeaseIpAddress) failed and returned "
  604. "%d", dwErr);
  605. goto LDone;
  606. }
  607. PDhcpRenewIpAddressLease = (DHCPRENEWIPADDRESSLEASE)
  608. GetProcAddress(HelperDhcpDll, "DhcpRenewIpAddressLease");
  609. if (NULL == PDhcpRenewIpAddressLease)
  610. {
  611. dwErr = GetLastError();
  612. TraceHlp("GetProcAddress(DhcpRenewIpAddressLease) failed and returned "
  613. "%d", dwErr);
  614. goto LDone;
  615. }
  616. PDhcpReleaseIpAddressLease = (DHCPRELEASEIPADDRESSLEASE)
  617. GetProcAddress(HelperDhcpDll, "DhcpReleaseIpAddressLease");
  618. if (NULL == PDhcpReleaseIpAddressLease)
  619. {
  620. dwErr = GetLastError();
  621. TraceHlp("GetProcAddress(DhcpReleaseIpAddressLease) failed and "
  622. "returned %d", dwErr);
  623. goto LDone;
  624. }
  625. PAllocateAndGetIpAddrTableFromStack = (ALLOCATEANDGETIPADDRTABLEFROMSTACK)
  626. GetProcAddress(HelperIpHlpDll, "AllocateAndGetIpAddrTableFromStack");
  627. if (NULL == PAllocateAndGetIpAddrTableFromStack)
  628. {
  629. dwErr = GetLastError();
  630. TraceHlp("GetProcAddress(AllocateAndGetIpAddrTableFromStack) failed "
  631. "and returned %d", dwErr);
  632. goto LDone;
  633. }
  634. PSetProxyArpEntryToStack = (SETPROXYARPENTRYTOSTACK)
  635. GetProcAddress(HelperIpHlpDll, "SetProxyArpEntryToStack");
  636. if (NULL == PSetProxyArpEntryToStack)
  637. {
  638. dwErr = GetLastError();
  639. TraceHlp("GetProcAddress(SetProxyArpEntryToStack) failed and "
  640. "returned %d", dwErr);
  641. goto LDone;
  642. }
  643. PSetIpForwardEntryToStack = (SETIPFORWARDENTRYTOSTACK)
  644. GetProcAddress(HelperIpHlpDll, "SetIpForwardEntryToStack");
  645. if (NULL == PSetIpForwardEntryToStack)
  646. {
  647. dwErr = GetLastError();
  648. TraceHlp("GetProcAddress(SetIpForwardEntryToStack) failed and "
  649. "returned %d", dwErr);
  650. goto LDone;
  651. }
  652. PSetIpForwardEntry = (SETIPFORWARDENTRY)
  653. GetProcAddress(HelperIpHlpDll, "SetIpForwardEntry");
  654. if (NULL == PSetIpForwardEntry)
  655. {
  656. dwErr = GetLastError();
  657. TraceHlp("GetProcAddress(SetIpForwardEntry) failed and "
  658. "returned %d", dwErr);
  659. goto LDone;
  660. }
  661. PDeleteIpForwardEntry = (DELETEIPFORWARDENTRY)
  662. GetProcAddress(HelperIpHlpDll, "DeleteIpForwardEntry");
  663. if (NULL == PDeleteIpForwardEntry)
  664. {
  665. dwErr = GetLastError();
  666. TraceHlp("GetProcAddress(DeleteIpForwardEntry) failed and "
  667. "returned %d", dwErr);
  668. goto LDone;
  669. }
  670. PNhpAllocateAndGetInterfaceInfoFromStack =
  671. (NHPALLOCATEANDGETINTERFACEINFOFROMSTACK)
  672. GetProcAddress(HelperIpHlpDll,
  673. "NhpAllocateAndGetInterfaceInfoFromStack");
  674. if (NULL == PNhpAllocateAndGetInterfaceInfoFromStack)
  675. {
  676. dwErr = GetLastError();
  677. TraceHlp("GetProcAddress(NhpAllocateAndGetInterfaceInfoFromStack) "
  678. "failed and returned %d", dwErr);
  679. goto LDone;
  680. }
  681. PAllocateAndGetIpForwardTableFromStack =
  682. (ALLOCATEANDGETIPFORWARDTABLEFROMSTACK)
  683. GetProcAddress(HelperIpHlpDll,
  684. "AllocateAndGetIpForwardTableFromStack");
  685. if (NULL == PAllocateAndGetIpForwardTableFromStack)
  686. {
  687. dwErr = GetLastError();
  688. TraceHlp("GetProcAddress(AllocateAndGetIpForwardTableFromStack) "
  689. "failed and returned %d", dwErr);
  690. goto LDone;
  691. }
  692. PGetAdaptersInfo = (GETADAPTERSINFO)
  693. GetProcAddress(HelperIpHlpDll, "GetAdaptersInfo");
  694. if (NULL == PGetAdaptersInfo)
  695. {
  696. dwErr = GetLastError();
  697. TraceHlp("GetProcAddress(GetAdaptersInfo) failed and "
  698. "returned %d", dwErr);
  699. goto LDone;
  700. }
  701. PGetPerAdapterInfo = (GETPERADAPTERINFO)
  702. GetProcAddress(HelperIpHlpDll, "GetPerAdapterInfo");
  703. if (NULL == PGetPerAdapterInfo)
  704. {
  705. dwErr = GetLastError();
  706. TraceHlp("GetProcAddress(GetPerAdapterInfo) failed and "
  707. "returned %d", dwErr);
  708. goto LDone;
  709. }
  710. if (NULL != HelperIpBootpDll)
  711. {
  712. PEnableDhcpInformServer = (ENABLEDHCPINFORMSERVER)
  713. GetProcAddress(HelperIpBootpDll, "EnableDhcpInformServer");
  714. if (NULL == PEnableDhcpInformServer)
  715. {
  716. dwErr = GetLastError();
  717. TraceHlp("GetProcAddress(EnableDhcpInformServer) failed and "
  718. "returned %d", dwErr);
  719. goto LDone;
  720. }
  721. PDisableDhcpInformServer = (DISABLEDHCPINFORMSERVER)
  722. GetProcAddress(HelperIpBootpDll, "DisableDhcpInformServer");
  723. if (NULL == PDisableDhcpInformServer)
  724. {
  725. dwErr = GetLastError();
  726. TraceHlp("GetProcAddress(DisableDhcpInformServer) failed and "
  727. "returned %d", dwErr);
  728. goto LDone;
  729. }
  730. }
  731. LDone:
  732. return(dwErr);
  733. }
  734. /*
  735. Returns:
  736. VOID
  737. Description:
  738. */
  739. VOID
  740. helperReadRegistry(
  741. VOID
  742. )
  743. {
  744. LONG lErr;
  745. DWORD dwErr;
  746. DWORD dw;
  747. DWORD dwNumBytes;
  748. CHAR* szIpAddr = NULL;
  749. CHAR* szAlloced = NULL;
  750. IPADDR nboIpAddr1;
  751. IPADDR nboIpAddr2;
  752. HKEY hKeyIpParam = NULL;
  753. WCHAR* wszAdapterGuid = NULL;
  754. HRESULT hr;
  755. HelperRegVal.fSuppressWINSNameServers = FALSE;
  756. HelperRegVal.fSuppressDNSNameServers = FALSE;
  757. HelperRegVal.dwChunkSize = 10;
  758. HelperRegVal.nboWINSNameServer1 = 0;
  759. HelperRegVal.nboWINSNameServer2 = 0;
  760. HelperRegVal.nboDNSNameServer1 = 0;
  761. HelperRegVal.nboDNSNameServer2 = 0;
  762. HelperRegVal.fNICChosen = FALSE;
  763. HelperRegVal.fEnableRoute = FALSE;
  764. lErr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_RAS_IP_PARAM_A, 0,
  765. KEY_READ, &hKeyIpParam);
  766. if (ERROR_SUCCESS == lErr)
  767. {
  768. dwErr = RegQueryValueWithAllocW(hKeyIpParam, REGVAL_ADAPTERGUID_W,
  769. REG_SZ, (BYTE**)&wszAdapterGuid);
  770. if ( (NO_ERROR == dwErr)
  771. && (wszAdapterGuid[0]))
  772. {
  773. hr = CLSIDFromString(wszAdapterGuid, &(HelperRegVal.guidChosenNIC));
  774. if (!FAILED(hr))
  775. {
  776. HelperRegVal.fNICChosen = TRUE;
  777. }
  778. }
  779. dwNumBytes = sizeof(dw);
  780. lErr = RegQueryValueEx(hKeyIpParam, REGVAL_SUPPRESSWINS_A, NULL, NULL,
  781. (BYTE*)&dw, &dwNumBytes);
  782. if ( (ERROR_SUCCESS == lErr)
  783. && (0 != dw))
  784. {
  785. HelperRegVal.fSuppressWINSNameServers = TRUE;
  786. }
  787. dwNumBytes = sizeof(dw);
  788. lErr = RegQueryValueEx(hKeyIpParam, REGVAL_SUPPRESSDNS_A, NULL, NULL,
  789. (BYTE*)&dw, &dwNumBytes);
  790. if ( (ERROR_SUCCESS == lErr)
  791. && (0 != dw))
  792. {
  793. HelperRegVal.fSuppressDNSNameServers = TRUE;
  794. }
  795. dwNumBytes = sizeof(dw);
  796. lErr = RegQueryValueEx(hKeyIpParam, REGVAL_CHUNK_SIZE_A, NULL, NULL,
  797. (BYTE*)&dw, &dwNumBytes);
  798. if (ERROR_SUCCESS == lErr)
  799. {
  800. HelperRegVal.dwChunkSize = dw;
  801. }
  802. dwNumBytes = sizeof(dw);
  803. lErr = RegQueryValueEx(hKeyIpParam, REGVAL_ALLOW_NETWORK_ACCESS_A, NULL,
  804. NULL, (BYTE*)&dw, &dwNumBytes);
  805. if (ERROR_SUCCESS == lErr)
  806. {
  807. HelperRegVal.fEnableRoute = dw;
  808. }
  809. dwErr = RegQueryValueWithAllocA(hKeyIpParam, REGVAL_WINSSERVER_A,
  810. REG_SZ, &szIpAddr);
  811. if (NO_ERROR == dwErr)
  812. {
  813. nboIpAddr1 = inet_addr(szIpAddr);
  814. if (INADDR_NONE != nboIpAddr1)
  815. {
  816. HelperRegVal.nboWINSNameServer1 = nboIpAddr1;
  817. }
  818. LocalFree(szIpAddr);
  819. szIpAddr = NULL;
  820. }
  821. dwErr = RegQueryValueWithAllocA(hKeyIpParam, REGVAL_WINSSERVERBACKUP_A,
  822. REG_SZ, &szIpAddr);
  823. if (NO_ERROR == dwErr)
  824. {
  825. nboIpAddr2 = inet_addr(szIpAddr);
  826. if ( (INADDR_NONE != nboIpAddr2)
  827. && (0 != HelperRegVal.nboWINSNameServer1))
  828. {
  829. HelperRegVal.nboWINSNameServer2 = nboIpAddr2;
  830. }
  831. LocalFree(szIpAddr);
  832. szIpAddr = NULL;
  833. }
  834. dwErr = RegQueryValueWithAllocA(hKeyIpParam, REGVAL_DNSSERVERS_A,
  835. REG_MULTI_SZ, &szAlloced);
  836. if (NO_ERROR == dwErr)
  837. {
  838. szIpAddr = szAlloced;
  839. nboIpAddr1 = inet_addr(szIpAddr);
  840. // We are sure that the buffer szIpAddr has 2 zeros at the end
  841. szIpAddr += strlen(szIpAddr) + 1;
  842. nboIpAddr2 = inet_addr(szIpAddr);
  843. if ( (INADDR_NONE != nboIpAddr1)
  844. && (0 != nboIpAddr1))
  845. {
  846. HelperRegVal.nboDNSNameServer1 = nboIpAddr1;
  847. if (INADDR_NONE != nboIpAddr2)
  848. {
  849. HelperRegVal.nboDNSNameServer2 = nboIpAddr2;
  850. }
  851. }
  852. LocalFree(szAlloced);
  853. szAlloced = NULL;
  854. }
  855. }
  856. TraceHlp("%s: %d, %s: %d, %s: %d",
  857. REGVAL_SUPPRESSWINS_A, HelperRegVal.fSuppressWINSNameServers,
  858. REGVAL_SUPPRESSDNS_A, HelperRegVal.fSuppressDNSNameServers,
  859. REGVAL_CHUNK_SIZE_A, HelperRegVal.dwChunkSize);
  860. TraceHlp("%s: 0x%x, %s: 0x%x, %s: 0x%x, 0x%x",
  861. REGVAL_WINSSERVER_A, HelperRegVal.nboWINSNameServer1,
  862. REGVAL_WINSSERVERBACKUP_A, HelperRegVal.nboWINSNameServer2,
  863. REGVAL_DNSSERVERS_A, HelperRegVal.nboDNSNameServer1,
  864. HelperRegVal.nboDNSNameServer2);
  865. if (NULL != hKeyIpParam)
  866. {
  867. RegCloseKey(hKeyIpParam);
  868. }
  869. LocalFree(wszAdapterGuid);
  870. }