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.

1074 lines
30 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C R A S . C P P
  7. //
  8. // Contents: Common code for RAS connections.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 20 Oct 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ncnetcon.h"
  18. #include "ncras.h"
  19. #include "ncstring.h"
  20. #include <raserror.h>
  21. #include "mprapi.h"
  22. //+---------------------------------------------------------------------------
  23. //
  24. // Function: RciFree
  25. //
  26. // Purpose: Frees the memory associated with a RASCON_INFO strucutre.
  27. //
  28. // Arguments:
  29. // pRasConInfo [in] Pointer to RASCON_INFO structure to free.
  30. //
  31. // Returns: nothing
  32. //
  33. // Author: shaunco 21 Sep 1997
  34. //
  35. // Notes: CoTaskMemAlloc/CoTaskMemFree are used as the allocator/
  36. // deallocator because this structure is marshalled by COM.
  37. //
  38. VOID
  39. RciFree (
  40. RASCON_INFO* pRasConInfo)
  41. {
  42. Assert (pRasConInfo);
  43. CoTaskMemFree (pRasConInfo->pszwPbkFile);
  44. CoTaskMemFree (pRasConInfo->pszwEntryName);
  45. ZeroMemory (pRasConInfo, sizeof (*pRasConInfo));
  46. }
  47. //+---------------------------------------------------------------------------
  48. //
  49. // Function: FExistActiveRasConnections
  50. //
  51. // Purpose: Returns TRUE if there is at least one active RAS connection.
  52. // Both incoming and outgoing connections are checked.
  53. //
  54. // Arguments:
  55. // (none)
  56. //
  57. // Returns: TRUE if at least one incoming or outgoing RAS connection
  58. // in progress. FALSE if not.
  59. //
  60. // Author: shaunco 8 Jul 1998
  61. //
  62. // Notes:
  63. //
  64. BOOL
  65. FExistActiveRasConnections ()
  66. {
  67. BOOL fExist = FALSE;
  68. RASCONN RasConn;
  69. DWORD dwErr;
  70. DWORD cbBuf;
  71. DWORD cConnections;
  72. ZeroMemory (&RasConn, sizeof(RasConn));
  73. RasConn.dwSize = sizeof(RasConn);
  74. cbBuf = sizeof(RasConn);
  75. cConnections = 0;
  76. dwErr = RasEnumConnections (&RasConn, &cbBuf, &cConnections);
  77. if ((ERROR_SUCCESS == dwErr) || (ERROR_BUFFER_TOO_SMALL == dwErr))
  78. {
  79. fExist = (cbBuf > 0) || (cConnections > 0);
  80. }
  81. // If no outgoing connections active, check on incoming ones.
  82. //
  83. if (!fExist)
  84. {
  85. MPR_SERVER_HANDLE hMprServer;
  86. LPBYTE lpbBuf = NULL;
  87. DWORD dwEntriesRead = 0;
  88. DWORD dwTotalEntries = 0;
  89. ZeroMemory (&hMprServer, sizeof(hMprServer));
  90. //get a handle to the local router ie. name = NULL
  91. dwErr = MprAdminServerConnect( NULL, &hMprServer );
  92. if (ERROR_SUCCESS == dwErr)
  93. {
  94. //retrieve a pointer to buffer containing all
  95. //incoming connections (ie dwPrefMaxLen = -1) and
  96. //the their count ( ie. dwTotalEntries )
  97. dwErr = MprAdminConnectionEnum( hMprServer,
  98. 0,
  99. &lpbBuf,
  100. (DWORD)-1,
  101. &dwEntriesRead,
  102. &dwTotalEntries,
  103. NULL );
  104. if (ERROR_SUCCESS == dwErr)
  105. {
  106. fExist = (dwTotalEntries > 0);
  107. if( lpbBuf )
  108. {
  109. MprAdminBufferFree( lpbBuf );
  110. lpbBuf = NULL;
  111. }
  112. }
  113. // close the handle to the router
  114. MprAdminServerDisconnect( hMprServer );
  115. }
  116. }
  117. return fExist;
  118. }
  119. //+---------------------------------------------------------------------------
  120. //
  121. // Function: HrRciGetRasConnectionInfo
  122. //
  123. // Purpose: QI an INetConnection pointer for INetRasConnection and make
  124. // a call to GetRasConnectionInfo on it.
  125. //
  126. // Arguments:
  127. // pCon [in] The connection to QI and call.
  128. // pRasConInfo [out] The returned information.
  129. //
  130. // Returns: S_OK or an error code.
  131. //
  132. // Author: shaunco 15 Nov 1997
  133. //
  134. // Notes:
  135. //
  136. HRESULT
  137. HrRciGetRasConnectionInfo (
  138. INetConnection* pCon,
  139. RASCON_INFO* pRasConInfo)
  140. {
  141. INetRasConnection* pRasCon;
  142. HRESULT hr = HrQIAndSetProxyBlanket(pCon, &pRasCon);
  143. if (S_OK == hr)
  144. {
  145. // Make the call to get the info and release the interface.
  146. //
  147. hr = pRasCon->GetRasConnectionInfo (pRasConInfo);
  148. ReleaseObj (pRasCon);
  149. }
  150. TraceError ("HrRciGetRasConnectionInfo", hr);
  151. return hr;
  152. }
  153. //+---------------------------------------------------------------------------
  154. //
  155. // Function: HrRasEnumAllActiveConnections
  156. //
  157. // Purpose: Enumerate and return all active RAS connections.
  158. //
  159. // Arguments:
  160. // paRasConn [out] Pointer to returned allocation of RASCONN structures.
  161. // pcRasConn [out] Pointer to count of RASCONN structures returned.
  162. //
  163. // Returns: S_OK or an error code.
  164. //
  165. // Author: shaunco 23 Sep 1997
  166. //
  167. // Notes: The returned buffer must be freed using free.
  168. //
  169. HRESULT
  170. HrRasEnumAllActiveConnections (
  171. RASCONN** paRasConn,
  172. DWORD* pcRasConn)
  173. {
  174. // Allocate room for two active connections initially. We'll update
  175. // this guess after we successfully allocate and find out how much was
  176. // really needed. Saving it across calls will keep us from allocating
  177. // too much or too little.
  178. //
  179. static DWORD cbBufGuess = 2 * sizeof (RASCONN);
  180. DWORD cbBuf = cbBufGuess;
  181. BOOL fRetry = TRUE;
  182. // Initialize the output parameters.
  183. //
  184. *paRasConn = NULL;
  185. *pcRasConn = NULL;
  186. // Allocate cbBuf bytes.
  187. //
  188. allocate:
  189. HRESULT hr = E_OUTOFMEMORY;
  190. RASCONN* aRasConn = reinterpret_cast<RASCONN*>(MemAlloc (cbBuf));
  191. if (aRasConn)
  192. {
  193. aRasConn->dwSize = sizeof (RASCONN);
  194. DWORD cRasConn;
  195. DWORD dwErr = RasEnumConnections (aRasConn, &cbBuf, &cRasConn);
  196. hr = HRESULT_FROM_WIN32 (dwErr);
  197. if (SUCCEEDED(hr))
  198. {
  199. // Update our guess for next time to be one more than we got back
  200. // this time.
  201. //
  202. cbBufGuess = cbBuf + sizeof (RASCONN);
  203. if (cRasConn)
  204. {
  205. *paRasConn = aRasConn;
  206. *pcRasConn = cRasConn;
  207. }
  208. else
  209. {
  210. MemFree (aRasConn);
  211. }
  212. }
  213. else
  214. {
  215. MemFree (aRasConn);
  216. if (ERROR_BUFFER_TOO_SMALL == dwErr)
  217. {
  218. TraceTag (ttidWanCon, "Perf: Guessed buffer size incorrectly "
  219. "calling RasEnumConnections.\n"
  220. " Guessed %d, needed %d.", cbBufGuess, cbBuf);
  221. // In case RAS makes more calls by the time we get back
  222. // to enumerate with the bigger buffer, add room for a few
  223. // more.
  224. //
  225. cbBuf += 2 * sizeof (RASCONN);
  226. // Protect from an infinte loop by only retrying once.
  227. //
  228. if (fRetry)
  229. {
  230. fRetry = FALSE;
  231. goto allocate;
  232. }
  233. }
  234. }
  235. }
  236. TraceError ("HrRasEnumAllActiveConnections", hr);
  237. return hr;
  238. }
  239. //+---------------------------------------------------------------------------
  240. //
  241. // Function: HrRasEnumAllActiveServerConnections
  242. //
  243. // Purpose: Enumerate and return all active RAS server connections.
  244. //
  245. // Arguments:
  246. // paRasSrvConn [out] Pointer to returned allocation of RASSRVCONN
  247. // structures.
  248. // pcRasSrvConn [out] Pointer to count of RASSRVCONN structures
  249. // returned.
  250. //
  251. // Returns: S_OK or an error code.
  252. //
  253. // Author: shaunco 12 Nov 1997
  254. //
  255. // Notes: The returned buffer must be freed using free.
  256. //
  257. HRESULT
  258. HrRasEnumAllActiveServerConnections (
  259. RASSRVCONN** paRasSrvConn,
  260. DWORD* pcRasSrvConn)
  261. {
  262. // Allocate room for two active connections initially. We'll update
  263. // this guess after we successfully allocate and find out how much was
  264. // really needed. Saving it across calls will keep us from allocating
  265. // too much or too little.
  266. //
  267. static DWORD cbBufGuess = 2 * sizeof (RASSRVCONN);
  268. DWORD cbBuf = cbBufGuess;
  269. BOOL fRetry = TRUE;
  270. // Initialize the output parameters.
  271. //
  272. *paRasSrvConn = NULL;
  273. *pcRasSrvConn = NULL;
  274. // Allocate cbBuf bytes.
  275. //
  276. allocate:
  277. HRESULT hr = E_OUTOFMEMORY;
  278. RASSRVCONN* aRasSrvConn = reinterpret_cast<RASSRVCONN*>(MemAlloc (cbBuf));
  279. if (aRasSrvConn)
  280. {
  281. aRasSrvConn->dwSize = sizeof (RASSRVCONN);
  282. DWORD cRasSrvConn;
  283. DWORD dwErr = RasSrvEnumConnections (aRasSrvConn, &cbBuf, &cRasSrvConn);
  284. hr = HRESULT_FROM_WIN32 (dwErr);
  285. if (SUCCEEDED(hr))
  286. {
  287. // Update our guess for next time to be one more than we got back
  288. // this time.
  289. //
  290. cbBufGuess = cbBuf + sizeof (RASSRVCONN);
  291. if (cRasSrvConn)
  292. {
  293. *paRasSrvConn = aRasSrvConn;
  294. *pcRasSrvConn = cRasSrvConn;
  295. }
  296. else
  297. {
  298. MemFree (aRasSrvConn);
  299. }
  300. }
  301. else
  302. {
  303. MemFree (aRasSrvConn);
  304. if (ERROR_BUFFER_TOO_SMALL == dwErr)
  305. {
  306. TraceTag (ttidWanCon, "Perf: Guessed buffer size incorrectly "
  307. "calling RasSrvEnumConnections.\n"
  308. " Guessed %d, needed %d.", cbBufGuess, cbBuf);
  309. // In case RAS makes more calls by the time we get back
  310. // to enumerate with the bigger buffer, add room for a few
  311. // more.
  312. //
  313. cbBuf += 2 * sizeof (RASSRVCONN);
  314. // Protect from an infinte loop by only retrying once.
  315. //
  316. if (fRetry)
  317. {
  318. fRetry = FALSE;
  319. goto allocate;
  320. }
  321. }
  322. }
  323. }
  324. TraceError ("HrRasEnumAllActiveServerConnections", hr);
  325. return hr;
  326. }
  327. //+---------------------------------------------------------------------------
  328. //
  329. // Function: HrRasEnumAllEntriesWithDetails
  330. //
  331. // Purpose: Enumerate and return all RAS entries in a phone book.
  332. //
  333. // Arguments:
  334. // pszPhonebook [in] Phonebook file to use.
  335. // paRasEntryDetails [out] Pointer to returned allocation of
  336. // RASENUMENTRYDETAILS structures.
  337. // pcRasEntryDetails [out] Pointer to count of RASENUMENTRYDETAILS
  338. // structures returned.
  339. //
  340. // Returns: S_OK, S_FALSE if no entries, or an error code.
  341. //
  342. // Author: shaunco 2 Oct 1997
  343. //
  344. // Notes: The returned buffer must be freed using free.
  345. //
  346. HRESULT
  347. HrRasEnumAllEntriesWithDetails (
  348. PCWSTR pszPhonebook,
  349. RASENUMENTRYDETAILS** paRasEntryDetails,
  350. DWORD* pcRasEntryDetails)
  351. {
  352. // Allocate room for five entry names initially. We'll update
  353. // this guess after we successfully allocate and find out how much was
  354. // really needed. Saving it across calls will keep us from allocating
  355. // too much or too little.
  356. //
  357. static DWORD cbBufGuess = 5 * sizeof (RASENUMENTRYDETAILS);
  358. DWORD cbBuf = cbBufGuess;
  359. BOOL fRetry = TRUE;
  360. // Initialize the output parameters.
  361. //
  362. *paRasEntryDetails = NULL;
  363. *pcRasEntryDetails = 0;
  364. // Allocate cbBuf bytes.
  365. //
  366. allocate:
  367. HRESULT hr = E_OUTOFMEMORY;
  368. RASENUMENTRYDETAILS* aRasEntryDetails =
  369. reinterpret_cast<RASENUMENTRYDETAILS*>(MemAlloc (cbBuf));
  370. if (aRasEntryDetails)
  371. {
  372. ZeroMemory(aRasEntryDetails, cbBuf);
  373. aRasEntryDetails->dwSize = sizeof (RASENUMENTRYDETAILS);
  374. DWORD cRasEntryDetails;
  375. DWORD dwErr = DwEnumEntryDetails (
  376. pszPhonebook,
  377. aRasEntryDetails,
  378. &cbBuf, &cRasEntryDetails);
  379. hr = HRESULT_FROM_WIN32 (dwErr);
  380. if (SUCCEEDED(hr))
  381. {
  382. // Update our guess for next time to be one more than we got back
  383. // this time.
  384. //
  385. cbBufGuess = cbBuf + sizeof (RASENUMENTRYDETAILS);
  386. if (cRasEntryDetails)
  387. {
  388. *paRasEntryDetails = aRasEntryDetails;
  389. *pcRasEntryDetails = cRasEntryDetails;
  390. }
  391. else
  392. {
  393. MemFree (aRasEntryDetails);
  394. hr = S_FALSE;
  395. }
  396. }
  397. else
  398. {
  399. MemFree (aRasEntryDetails);
  400. if (ERROR_BUFFER_TOO_SMALL == dwErr)
  401. {
  402. TraceTag (ttidWanCon, "Perf: Guessed buffer size incorrectly "
  403. "calling DwEnumEntryDetails.\n"
  404. " Guessed %d, needed %d.", cbBufGuess, cbBuf);
  405. // Protect from an infinte loop by only retrying once.
  406. //
  407. if (fRetry)
  408. {
  409. fRetry = FALSE;
  410. goto allocate;
  411. }
  412. }
  413. }
  414. }
  415. TraceError ("HrRasEnumAllEntriesWithDetails", (S_FALSE == hr) ? S_OK : hr);
  416. return hr;
  417. }
  418. //+---------------------------------------------------------------------------
  419. //
  420. // Function: HrFindRasConnFromGuidId
  421. //
  422. // Purpose: Searches for the active RAS connection that corresponds to
  423. // the phone book entry given by a GUID.
  424. //
  425. // Arguments:
  426. // pguid [in] Pointer to the GUID which identifies the entry.
  427. // phRasConn [out] The returned handle to the RAS connection if it
  428. // was found. NULL otherwise.
  429. // pRasConn [out] Optional pointer to returned RASCONN structure
  430. // if found.
  431. //
  432. // Returns: S_OK if found, S_FALSE if not, or an error code.
  433. //
  434. // Author: shaunco 29 Sep 1997
  435. //
  436. // Notes:
  437. //
  438. HRESULT
  439. HrFindRasConnFromGuidId (
  440. IN const GUID* pguid,
  441. OUT HRASCONN* phRasConn,
  442. OUT RASCONN* pRasConn OPTIONAL)
  443. {
  444. Assert (pguid);
  445. Assert (phRasConn);
  446. HRESULT hr;
  447. RASCONN* aRasConn;
  448. DWORD cRasConn;
  449. // Initialize the output parameter.
  450. //
  451. *phRasConn = NULL;
  452. hr = HrRasEnumAllActiveConnections (&aRasConn, &cRasConn);
  453. if (S_OK == hr)
  454. {
  455. hr = S_FALSE;
  456. for (DWORD i = 0; i < cRasConn; i++)
  457. {
  458. if (*pguid == aRasConn[i].guidEntry)
  459. {
  460. *phRasConn = aRasConn[i].hrasconn;
  461. if (pRasConn)
  462. {
  463. CopyMemory (pRasConn, &aRasConn[i], sizeof(RASCONN));
  464. }
  465. hr = S_OK;
  466. break;
  467. }
  468. }
  469. MemFree (aRasConn);
  470. }
  471. TraceHr (ttidError, FAL, hr, S_FALSE == hr, "HrFindRasConnFromGuidId");
  472. return hr;
  473. }
  474. //+---------------------------------------------------------------------------
  475. //
  476. // Function: HrRasGetEntryProperties
  477. //
  478. // Purpose: Wrapper around RasGetEntryProperties that returns an HRESULT
  479. // and allocates the necessary memory automatically.
  480. //
  481. // Arguments:
  482. // pszPhonebook [in]
  483. // pszEntry [in]
  484. // ppRasEntry [out]
  485. //
  486. // Returns: S_OK or an error code.
  487. //
  488. // Author: shaunco 6 Oct 1997
  489. //
  490. // Notes: The output parameters should be freed using free.
  491. //
  492. HRESULT
  493. HrRasGetEntryProperties (
  494. PCWSTR pszPhonebook,
  495. PCWSTR pszEntry,
  496. RASENTRY** ppRasEntry,
  497. DWORD* pcbRasEntry)
  498. {
  499. // Init the output parameter if provided.
  500. //
  501. if (pcbRasEntry)
  502. {
  503. *pcbRasEntry = 0;
  504. }
  505. // Allocate room for RASENTRY structure plus 256 bytes initially.
  506. // We'll update this guess after we successfully allocate and find out
  507. // how much was really needed. Saving it across calls will keep us
  508. // from allocating too much or too little.
  509. //
  510. static DWORD cbBufGuess = sizeof (RASENTRY) + 256;
  511. DWORD cbBuf = cbBufGuess;
  512. BOOL fRetry = TRUE;
  513. // Initialize the output parameters.
  514. //
  515. *ppRasEntry = NULL;
  516. if (pcbRasEntry)
  517. {
  518. *pcbRasEntry = 0;
  519. }
  520. // Allocate cbBuf bytes.
  521. //
  522. allocate:
  523. HRESULT hr = E_OUTOFMEMORY;
  524. RASENTRY* pRasEntry = reinterpret_cast<RASENTRY*>(MemAlloc (cbBuf));
  525. if (pRasEntry)
  526. {
  527. pRasEntry->dwSize = sizeof (RASENTRY);
  528. DWORD dwErr = RasGetEntryProperties (pszPhonebook, pszEntry,
  529. pRasEntry, &cbBuf, NULL, NULL);
  530. hr = HRESULT_FROM_WIN32 (dwErr);
  531. if (SUCCEEDED(hr))
  532. {
  533. // Update our guess for next time to be a bit more than we
  534. // got back this time.
  535. //
  536. cbBufGuess = cbBuf + 256;
  537. *ppRasEntry = pRasEntry;
  538. if (pcbRasEntry)
  539. {
  540. *pcbRasEntry = cbBuf;
  541. }
  542. }
  543. else
  544. {
  545. MemFree (pRasEntry);
  546. if (ERROR_BUFFER_TOO_SMALL == dwErr)
  547. {
  548. TraceTag (ttidWanCon, "Perf: Guessed buffer size incorrectly "
  549. "calling RasGetEntryProperties.\n"
  550. " Guessed %d, needed %d.", cbBufGuess, cbBuf);
  551. // Protect from an infinte loop by only retrying once.
  552. //
  553. if (fRetry)
  554. {
  555. fRetry = FALSE;
  556. goto allocate;
  557. }
  558. }
  559. }
  560. }
  561. TraceError ("HrRasGetEntryProperties", hr);
  562. return hr;
  563. }
  564. //+---------------------------------------------------------------------------
  565. //
  566. // Function: HrRasGetSubEntryProperties
  567. //
  568. // Purpose: Wrapper around RasGetSubEntryProperties that returns an HRESULT
  569. // and allocates the necessary memory automatically.
  570. //
  571. // Arguments:
  572. // pszPhonebook [in]
  573. // pszEntry [in]
  574. // dwSubEntry [in]
  575. // ppRasSubEntry [out]
  576. //
  577. // Returns: S_OK or an error code.
  578. //
  579. // Author: CWill 02/10/98
  580. //
  581. // Notes: The output parameters should be freed using free.
  582. //
  583. HRESULT
  584. HrRasGetSubEntryProperties (
  585. PCWSTR pszPhonebook,
  586. PCWSTR pszEntry,
  587. DWORD dwSubEntry,
  588. RASSUBENTRY** ppRasSubEntry)
  589. {
  590. // Allocate room for RASSUBENTRY structure plus 256 bytes initially.
  591. // We'll update this guess after we successfully allocate and find out
  592. // how much was really needed. Saving it across calls will keep us
  593. // from allocating too much or too little.
  594. //
  595. static DWORD cbBufGuess = sizeof (RASSUBENTRY) + 256;
  596. DWORD cbBuf = cbBufGuess;
  597. BOOL fRetry = TRUE;
  598. // Initialize the output parameters.
  599. //
  600. *ppRasSubEntry = NULL;
  601. // Allocate cbBuf bytes.
  602. //
  603. allocate:
  604. HRESULT hr = E_OUTOFMEMORY;
  605. RASSUBENTRY* pRasSubEntry = reinterpret_cast<RASSUBENTRY*>(MemAlloc (cbBuf));
  606. if (pRasSubEntry)
  607. {
  608. pRasSubEntry->dwSize = sizeof (RASSUBENTRY);
  609. DWORD dwErr = RasGetSubEntryProperties (pszPhonebook, pszEntry,
  610. dwSubEntry, pRasSubEntry, &cbBuf, NULL, NULL);
  611. hr = HRESULT_FROM_WIN32 (dwErr);
  612. if (SUCCEEDED(hr))
  613. {
  614. // Update our guess for next time to be a bit more than we
  615. // got back this time.
  616. //
  617. cbBufGuess = cbBuf + 256;
  618. *ppRasSubEntry = pRasSubEntry;
  619. }
  620. else
  621. {
  622. MemFree (pRasSubEntry);
  623. if (ERROR_BUFFER_TOO_SMALL == dwErr)
  624. {
  625. TraceTag (ttidWanCon, "Perf: Guessed buffer size incorrectly "
  626. "calling RasGetSubEntryProperties.\n"
  627. " Guessed %d, needed %d.", cbBufGuess, cbBuf);
  628. // Protect from an infinte loop by only retrying once.
  629. //
  630. if (fRetry)
  631. {
  632. fRetry = FALSE;
  633. goto allocate;
  634. }
  635. }
  636. }
  637. }
  638. TraceError ("HrRasGetSubEntryProperties", hr);
  639. return hr;
  640. }
  641. //+---------------------------------------------------------------------------
  642. //
  643. // Function: HrRasGetNetconStatusFromRasConnectStatus
  644. //
  645. // Purpose: Returns a NETCON_STATUS value given a handle to a RAS
  646. // connection by calling RasGetConnectStatus and mapping.
  647. //
  648. // Arguments:
  649. // hRasConn [in] Handle to the RAS connection. (See Win32 RAS APIs.)
  650. // pStatus [out] Pointer to where the NETCON_STATUS is returned.
  651. //
  652. // Returns: S_OK or an error code in the FACILITY_WIN32 facility.
  653. //
  654. // Author: shaunco 6 May 1998
  655. //
  656. // Notes:
  657. //
  658. HRESULT
  659. HrRasGetNetconStatusFromRasConnectStatus (
  660. HRASCONN hRasConn,
  661. NETCON_STATUS* pStatus)
  662. {
  663. Assert (pStatus);
  664. // Initialize the output parameter.
  665. //
  666. *pStatus = NCS_DISCONNECTED;
  667. // Get its status and map it to our status.
  668. //
  669. RASCONNSTATUS RasConnStatus;
  670. ZeroMemory (&RasConnStatus, sizeof(RasConnStatus));
  671. RasConnStatus.dwSize = sizeof(RASCONNSTATUS);
  672. DWORD dwErr = RasGetConnectStatus (hRasConn, &RasConnStatus);
  673. HRESULT hr = HRESULT_FROM_WIN32 (dwErr);
  674. TraceHr (ttidError, FAL, hr,
  675. (HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE) == hr),
  676. "RasGetConnectStatus");
  677. if (S_OK == hr)
  678. {
  679. if (RasConnStatus.rasconnstate & RASCS_DONE)
  680. {
  681. if (RASCS_Disconnected != RasConnStatus.rasconnstate)
  682. {
  683. *pStatus = NCS_CONNECTED;
  684. }
  685. }
  686. else
  687. {
  688. *pStatus = NCS_CONNECTING;
  689. }
  690. }
  691. TraceHr (ttidError, FAL, hr,
  692. (HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE) == hr),
  693. "HrRasGetNetconStatusFromRasConnectStatus");
  694. return hr;
  695. }
  696. //+---------------------------------------------------------------------------
  697. //
  698. // Function: HrRasHangupUntilDisconnected
  699. //
  700. // Purpose: Call RasHangup until the connection is disconnected.
  701. // Ras reference-counts RasDial/RasHangup, so this is called
  702. // when the connection needs to be dropped no matter what.
  703. // (For example, the behavior of disconnect from the shell
  704. // is to drop the connection regardless of who dialed it.)
  705. //
  706. // Arguments:
  707. // hRasConn [in] The connection to disconnect.
  708. //
  709. // Returns: S_OK or an error code.
  710. //
  711. // Author: shaunco 29 May 1999
  712. //
  713. HRESULT
  714. HrRasHangupUntilDisconnected (
  715. IN HRASCONN hRasConn)
  716. {
  717. HRESULT hr = E_UNEXPECTED;
  718. BOOL fDisconnectAgain;
  719. do
  720. {
  721. fDisconnectAgain = FALSE;
  722. // Hang up.
  723. //
  724. DWORD dwErr = RasHangUp (hRasConn);
  725. hr = HRESULT_FROM_WIN32 (dwErr);
  726. TraceError ("RasHangUp", hr);
  727. if (SUCCEEDED(hr))
  728. {
  729. // Since the connection may be ref-counted, see if
  730. // it's still connected and, if it is, go back and
  731. // disconnect again.
  732. //
  733. HRESULT hrT;
  734. NETCON_STATUS Status;
  735. hrT = HrRasGetNetconStatusFromRasConnectStatus (
  736. hRasConn,
  737. &Status);
  738. if ((S_OK == hrT) && (NCS_CONNECTED == Status))
  739. {
  740. fDisconnectAgain = TRUE;
  741. TraceTag (ttidWanCon, "need to disconnect again...");
  742. }
  743. }
  744. } while (fDisconnectAgain);
  745. TraceHr (ttidError, FAL, hr, FALSE, "HrRasHangupUntilDisconnected");
  746. return hr;
  747. }
  748. //+---------------------------------------------------------------------------
  749. //
  750. // Function: HrRasNetConToSharedConnection
  751. //
  752. // Purpose: Converts an 'INetConnection' to the format expected
  753. // by the RAS sharing API routines.
  754. //
  755. // Arguments:
  756. // pCon [in]
  757. // prcs [out]
  758. //
  759. // Returns: S_OK or an error code.
  760. //
  761. // Author: AboladeG 05/14/98
  762. //
  763. // Notes:
  764. //
  765. HRESULT
  766. HrNetConToSharedConnection (
  767. INetConnection* pCon,
  768. LPRASSHARECONN prsc)
  769. {
  770. HRESULT hr;
  771. NETCON_PROPERTIES* pProps;
  772. hr = pCon->GetProperties(&pProps);
  773. if (SUCCEEDED(hr))
  774. {
  775. if (pProps->MediaType == NCM_LAN)
  776. {
  777. RasGuidToSharedConnection(&pProps->guidId, prsc);
  778. }
  779. else
  780. {
  781. INetRasConnection* pnrc;
  782. hr = HrQIAndSetProxyBlanket(pCon, &pnrc);
  783. if (SUCCEEDED(hr))
  784. {
  785. RASCON_INFO rci;
  786. hr = pnrc->GetRasConnectionInfo (&rci);
  787. if (SUCCEEDED(hr))
  788. {
  789. RasEntryToSharedConnection (
  790. rci.pszwPbkFile, rci.pszwEntryName, prsc );
  791. RciFree (&rci);
  792. }
  793. ReleaseObj (pnrc);
  794. }
  795. }
  796. FreeNetconProperties(pProps);
  797. }
  798. TraceError ("HrRasNetConToSharedConnection", hr);
  799. return hr;
  800. }
  801. //+---------------------------------------------------------------------------
  802. //
  803. // Function: HrRasIsSharedConnection
  804. //
  805. // Purpose: Wrapper around RasIsSharedConnection that returns an HRESULT.
  806. //
  807. // Arguments:
  808. // prsc [in]
  809. // pfShared [out]
  810. //
  811. // Returns: S_OK or an error code.
  812. //
  813. // Author: AboladeG 05/04/98
  814. //
  815. // Notes:
  816. //
  817. HRESULT
  818. HrRasIsSharedConnection (
  819. LPRASSHARECONN prsc,
  820. BOOL* pfShared)
  821. {
  822. *pfShared = FALSE;
  823. DWORD dwErr = RasIsSharedConnection (prsc, pfShared);
  824. HRESULT hr = HRESULT_FROM_WIN32 (dwErr);
  825. TraceError ("HrRasIsSharedConnection", hr);
  826. return hr;
  827. }
  828. #if 0
  829. //+---------------------------------------------------------------------------
  830. //
  831. // Function: HrRasQueryLanConnTable
  832. //
  833. // Purpose: Wrapper around RasQueryLanConnTable that returns an HRESULT.
  834. //
  835. // Arguments:
  836. // prsc [in]
  837. // ppLanTable [out,optional]
  838. // pdwLanCount [out]
  839. //
  840. // Returns: S_OK or an error code.
  841. //
  842. // Author: AboladeG 05/14/98
  843. //
  844. // Notes:
  845. //
  846. HRESULT
  847. HrRasQueryLanConnTable (
  848. LPRASSHARECONN prsc,
  849. NETCON_PROPERTIES** ppLanTable,
  850. LPDWORD pdwLanCount)
  851. {
  852. DWORD dwErr = RasQueryLanConnTable (prsc, (LPVOID*)ppLanTable, pdwLanCount);
  853. HRESULT hr = HRESULT_FROM_WIN32 (dwErr);
  854. TraceError ("HrRasQueryLanConnTable", hr);
  855. return hr;
  856. }
  857. //+---------------------------------------------------------------------------
  858. //
  859. // Function: HrRasShareConnection
  860. //
  861. // Purpose: Wrapper around RasShareConnection that returns an HRESULT.
  862. //
  863. // Arguments:
  864. // prsc [in]
  865. // pPrivateLanGuid [in,optional]
  866. //
  867. // Returns: S_OK or an error code.
  868. //
  869. // Author: AboladeG 05/14/98
  870. //
  871. // Notes:
  872. //
  873. HRESULT
  874. HrRasShareConnection (
  875. LPRASSHARECONN prsc,
  876. GUID* pPrivateLanGuid)
  877. {
  878. DWORD dwErr = RasShareConnection (prsc, pPrivateLanGuid);
  879. HRESULT hr = HRESULT_FROM_WIN32 (dwErr);
  880. TraceError ("HrRasShareConnection", hr);
  881. return hr;
  882. }
  883. //+---------------------------------------------------------------------------
  884. //
  885. // Function: HrRasUnshareConnection
  886. //
  887. // Purpose: Wrapper around HrRasUnshareConnection that returns an HRESULT.
  888. //
  889. // Arguments:
  890. // pfWasShared [out]
  891. //
  892. // Returns: S_OK or an error code.
  893. //
  894. // Author: AboladeG 05/14/98
  895. //
  896. // Notes:
  897. //
  898. HRESULT
  899. HrRasUnshareConnection (
  900. PBOOL pfWasShared)
  901. {
  902. HRESULT hr;
  903. DWORD dwErr = RasUnshareConnection (pfWasShared);
  904. hr = HRESULT_FROM_WIN32 (dwErr);
  905. TraceError ("HrRasUnshareConnection", hr);
  906. return hr;
  907. }
  908. #endif
  909. //+---------------------------------------------------------------------------
  910. //
  911. // Function: RasSrvTypeFromRasDeviceType
  912. //
  913. // Purpose: Converts from RASDEVICETYPE into an accepted incoming type
  914. //
  915. // Arguments:
  916. // rdt [in] the RasDeviceType
  917. //
  918. // Returns: RASSRVUI_Xxx type
  919. //
  920. // Author: ckotze 19 Apr 2001
  921. //
  922. // Notes:
  923. //
  924. DWORD RasSrvTypeFromRasDeviceType(RASDEVICETYPE rdt)
  925. {
  926. DWORD dwType = RASSRVUI_MODEM;
  927. TraceTag (ttidWanCon, "rdt:0x%08x, dwType:0x%08x",
  928. rdt,
  929. dwType);
  930. switch (LOWORD(rdt))
  931. {
  932. case RDT_PPPoE:
  933. dwType = RASSRVUI_MODEM;
  934. break;
  935. case RDT_Modem:
  936. case RDT_X25:
  937. dwType = RASSRVUI_MODEM;
  938. break;
  939. case RDT_Isdn:
  940. dwType = RASSRVUI_MODEM;
  941. break;
  942. case RDT_Serial:
  943. case RDT_FrameRelay:
  944. case RDT_Atm:
  945. case RDT_Sonet:
  946. case RDT_Sw56:
  947. dwType = RASSRVUI_MODEM;
  948. break;
  949. case RDT_Tunnel_Pptp:
  950. case RDT_Tunnel_L2tp:
  951. dwType = RASSRVUI_VPN;
  952. break;
  953. case RDT_Irda:
  954. case RDT_Parallel:
  955. dwType = RASSRVUI_DCC;
  956. break;
  957. case RDT_Other:
  958. default:
  959. dwType = RASSRVUI_MODEM;
  960. }
  961. if (rdt & RDT_Tunnel)
  962. {
  963. dwType = RASSRVUI_VPN;
  964. }
  965. else if (rdt & (RDT_Direct | RDT_Null_Modem))
  966. {
  967. dwType = RASSRVUI_DCC;
  968. }
  969. return dwType;
  970. }