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.

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