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.

1082 lines
24 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 2000
  5. //
  6. // File: H N A P I . C P P
  7. //
  8. // Contents: OEM API
  9. //
  10. // Notes:
  11. //
  12. // Author: billi 21 Nov 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include <winsock2.h>
  18. void __cdecl hnet_oem_trans_func( unsigned int uSECode, EXCEPTION_POINTERS* pExp )
  19. {
  20. throw HNet_Oem_SEH_Exception( uSECode );
  21. }
  22. void EnableOEMExceptionHandling()
  23. {
  24. _set_se_translator (hnet_oem_trans_func);
  25. }
  26. void DisableOEMExceptionHandling()
  27. {
  28. _set_se_translator(NULL);
  29. }
  30. HRESULT
  31. InternalGetSharingEnabled(
  32. IHNetConnection* pHNetConnection,
  33. BOOLEAN* pbEnabled,
  34. SHARINGCONNECTIONTYPE* pType
  35. )
  36. /*++
  37. InternalGetSharingEnabled
  38. Routine Description:
  39. Arguments:
  40. none
  41. Return Value:
  42. none
  43. --*/
  44. {
  45. HRESULT hr;
  46. HNET_CONN_PROPERTIES* pProps;
  47. if ( NULL == pHNetConnection )
  48. {
  49. hr = E_INVALIDARG;
  50. }
  51. else if ( ( NULL == pbEnabled ) || ( NULL == pType ) )
  52. {
  53. hr = E_POINTER;
  54. }
  55. else
  56. {
  57. *pbEnabled = FALSE;
  58. *pType = ICSSHARINGTYPE_PUBLIC;
  59. hr = pHNetConnection->GetProperties( &pProps );
  60. if ( SUCCEEDED(hr) )
  61. {
  62. if ( pProps->fIcsPublic )
  63. {
  64. *pbEnabled = TRUE;
  65. *pType = ICSSHARINGTYPE_PUBLIC;
  66. }
  67. else if ( pProps->fIcsPrivate )
  68. {
  69. *pbEnabled = TRUE;
  70. *pType = ICSSHARINGTYPE_PRIVATE;
  71. }
  72. CoTaskMemFree( pProps );
  73. }
  74. }
  75. return hr;
  76. }
  77. HRESULT
  78. InternalIsShareTypeEnabled(
  79. SHARINGCONNECTIONTYPE Type,
  80. BOOLEAN* pbEnabled
  81. )
  82. /*++
  83. InternalGetShareTypeEnabled
  84. Routine Description:
  85. Arguments:
  86. none
  87. Return Value:
  88. none
  89. --*/
  90. {
  91. HRESULT hr;
  92. if ( NULL == pbEnabled )
  93. {
  94. hr = E_POINTER;
  95. }
  96. else
  97. {
  98. IHNetIcsSettings* pIcsSettings;
  99. *pbEnabled = FALSE;
  100. hr = _ObtainIcsSettingsObj( &pIcsSettings );
  101. if ( SUCCEEDED(hr) )
  102. {
  103. switch( Type )
  104. {
  105. case ICSSHARINGTYPE_PUBLIC:
  106. {
  107. IEnumHNetIcsPublicConnections* pHNetEnumPub;
  108. hr = pIcsSettings->EnumIcsPublicConnections( &pHNetEnumPub );
  109. if ( SUCCEEDED(hr) )
  110. {
  111. IHNetIcsPublicConnection *pIHNetIcsPublic;
  112. if ( pHNetEnumPub->Next( 1, &pIHNetIcsPublic, NULL ) == S_OK )
  113. {
  114. *pbEnabled = TRUE;
  115. ReleaseObj( pIHNetIcsPublic );
  116. }
  117. ReleaseObj( pHNetEnumPub );
  118. }
  119. }
  120. break;
  121. case ICSSHARINGTYPE_PRIVATE:
  122. {
  123. IEnumHNetIcsPrivateConnections* pHNetEnumPrv;
  124. hr = pIcsSettings->EnumIcsPrivateConnections( &pHNetEnumPrv );
  125. if ( SUCCEEDED(hr) )
  126. {
  127. IHNetIcsPrivateConnection *pIHNetIcsPrivate;
  128. if ( pHNetEnumPrv->Next( 1, &pIHNetIcsPrivate, NULL ) == S_OK )
  129. {
  130. *pbEnabled = TRUE;
  131. ReleaseObj( pIHNetIcsPrivate );
  132. }
  133. ReleaseObj( pHNetEnumPrv );
  134. }
  135. }
  136. break;
  137. default:
  138. hr = E_UNEXPECTED;
  139. break;
  140. } // switch( Type )
  141. ReleaseObj( pIcsSettings );
  142. } // if ( SUCCEEDED(hr) )
  143. } // if ( NULL == pbEnabled )
  144. return hr;
  145. }
  146. STDMETHODIMP
  147. CNetSharingConfiguration::GetSharingEnabled(
  148. BOOLEAN* pbEnabled,
  149. SHARINGCONNECTIONTYPE* pType )
  150. /*++
  151. CNetSharingConfiguration::GetSharingEnabled
  152. Routine Description:
  153. Arguments:
  154. none
  155. Return Value:
  156. none
  157. --*/
  158. {
  159. HNET_OEM_API_ENTER
  160. HRESULT hr = S_OK;
  161. if ( ( NULL == pbEnabled ) || ( NULL == pType ) )
  162. {
  163. hr = E_POINTER;
  164. }
  165. else
  166. {
  167. *pbEnabled = FALSE;
  168. *pType = ICSSHARINGTYPE_PUBLIC;
  169. BOOLEAN bBridged = FALSE;
  170. hr = InternalGetSharingEnabled( m_pHNetConnection, pbEnabled, pType );
  171. }
  172. return hr;
  173. HNET_OEM_API_LEAVE
  174. }
  175. STDMETHODIMP CNetSharingConfiguration::get_SharingEnabled (VARIANT_BOOL* pbEnabled)
  176. {
  177. HNET_OEM_API_ENTER
  178. SHARINGCONNECTIONTYPE Type;
  179. BOOLEAN bEnabled = FALSE;
  180. HRESULT hr = GetSharingEnabled (&bEnabled, &Type);
  181. if (SUCCEEDED(hr))
  182. *pbEnabled = bEnabled ? VARIANT_TRUE : VARIANT_FALSE;
  183. return hr;
  184. HNET_OEM_API_LEAVE
  185. }
  186. STDMETHODIMP CNetSharingConfiguration::get_SharingConnectionType(SHARINGCONNECTIONTYPE* pType)
  187. {
  188. HNET_OEM_API_ENTER
  189. BOOLEAN bEnabled;
  190. return GetSharingEnabled (&bEnabled, pType);
  191. HNET_OEM_API_LEAVE
  192. }
  193. STDMETHODIMP
  194. CNetSharingConfiguration::DisableSharing()
  195. /*++
  196. CNetSharingConfiguration::DisableSharing
  197. Routine Description:
  198. Arguments:
  199. none
  200. Return Value:
  201. none
  202. --*/
  203. {
  204. HNET_OEM_API_ENTER
  205. HRESULT hr;
  206. if ( !IsNotifyApproved() )
  207. {
  208. hr = E_ACCESSDENIED;
  209. }
  210. else
  211. {
  212. BOOLEAN bEnabled = FALSE;
  213. SHARINGCONNECTIONTYPE Type;
  214. hr = InternalGetSharingEnabled( m_pHNetConnection, &bEnabled, &Type );
  215. if ( SUCCEEDED(hr) && bEnabled )
  216. {
  217. switch( Type )
  218. {
  219. case ICSSHARINGTYPE_PUBLIC:
  220. {
  221. IHNetIcsPublicConnection* pPublicConnection;
  222. hr = m_pHNetConnection->GetControlInterface(
  223. __uuidof(IHNetIcsPublicConnection),
  224. reinterpret_cast<void**>(&pPublicConnection) );
  225. if ( SUCCEEDED(hr) )
  226. {
  227. hr = pPublicConnection->Unshare();
  228. ReleaseObj(pPublicConnection);
  229. }
  230. }
  231. break;
  232. case ICSSHARINGTYPE_PRIVATE:
  233. {
  234. IHNetIcsPrivateConnection* pPrivateConnection;
  235. hr = m_pHNetConnection->GetControlInterface(
  236. __uuidof(IHNetIcsPrivateConnection),
  237. reinterpret_cast<void**>(&pPrivateConnection) );
  238. if ( SUCCEEDED(hr) )
  239. {
  240. hr = pPrivateConnection->RemoveFromIcs();
  241. ReleaseObj(pPrivateConnection);
  242. }
  243. }
  244. break;
  245. default:
  246. hr = E_UNEXPECTED;
  247. }
  248. }
  249. }
  250. return hr;
  251. HNET_OEM_API_LEAVE
  252. }
  253. STDMETHODIMP
  254. CNetSharingConfiguration::EnableSharing(
  255. SHARINGCONNECTIONTYPE Type
  256. )
  257. /*++
  258. CNetSharingConfiguration::EnableSharing
  259. Routine Description:
  260. Arguments:
  261. none
  262. Return Value:
  263. none
  264. --*/
  265. {
  266. HNET_OEM_API_ENTER
  267. HRESULT hr;
  268. if ( !IsNotifyApproved() )
  269. {
  270. hr = E_ACCESSDENIED;
  271. }
  272. else
  273. {
  274. BOOLEAN bEnabled = FALSE;
  275. SHARINGCONNECTIONTYPE existingType;
  276. hr = InternalGetSharingEnabled( m_pHNetConnection, &bEnabled, &existingType );
  277. // If all ready sharing the connection at the specified type then
  278. // return hresult.
  279. if ( SUCCEEDED(hr) && ( !bEnabled || (existingType != Type) ) )
  280. {
  281. BOOLEAN bTypeEnabled = FALSE;
  282. hr = InternalIsShareTypeEnabled( Type, &bTypeEnabled );
  283. // If we have another adapter all ready shared at the specified type
  284. // then return error
  285. if ( SUCCEEDED(hr) && bTypeEnabled )
  286. {
  287. hr = E_ANOTHERADAPTERSHARED;
  288. }
  289. else
  290. {
  291. if ( bEnabled )
  292. {
  293. DisableSharing();
  294. }
  295. switch( Type )
  296. {
  297. case ICSSHARINGTYPE_PUBLIC:
  298. {
  299. IHNetIcsPublicConnection* pPublicConnection;
  300. hr = m_pHNetConnection->SharePublic( &pPublicConnection );
  301. if ( SUCCEEDED(hr) )
  302. {
  303. ReleaseObj( pPublicConnection );
  304. }
  305. }
  306. break;
  307. case ICSSHARINGTYPE_PRIVATE:
  308. {
  309. IHNetIcsPrivateConnection* pPrivateConnection;
  310. hr = m_pHNetConnection->SharePrivate( &pPrivateConnection );
  311. if ( SUCCEEDED(hr) )
  312. {
  313. ReleaseObj(pPrivateConnection);
  314. }
  315. }
  316. break;
  317. default:
  318. hr = E_UNEXPECTED;
  319. } // switch( Type )
  320. } // if ( SUCCEEDED(hr) && !bEnabled )
  321. } // if ( SUCCEEDED(hr) && ( !bEnabled || (existingType != Type) )
  322. } // if ( !IsNotifyApproved() )
  323. return hr;
  324. HNET_OEM_API_LEAVE
  325. }
  326. HRESULT
  327. InternalGetFirewallEnabled(
  328. IHNetConnection* pHNetConnection,
  329. BOOLEAN* pbEnabled
  330. )
  331. /*++
  332. InternalGetFirewallEnabled
  333. Routine Description:
  334. Arguments:
  335. none
  336. Return Value:
  337. none
  338. --*/
  339. {
  340. HRESULT hr;
  341. HNET_CONN_PROPERTIES* pProps;
  342. if ( NULL == pHNetConnection )
  343. {
  344. hr = E_INVALIDARG;
  345. }
  346. else if ( NULL == pbEnabled )
  347. {
  348. hr = E_POINTER;
  349. }
  350. else
  351. {
  352. *pbEnabled = FALSE;
  353. hr = pHNetConnection->GetProperties( &pProps );
  354. if ( SUCCEEDED(hr) )
  355. {
  356. if ( pProps->fFirewalled )
  357. {
  358. *pbEnabled = TRUE;
  359. }
  360. CoTaskMemFree( pProps );
  361. }
  362. }
  363. return hr;
  364. }
  365. STDMETHODIMP
  366. CNetSharingConfiguration::get_InternetFirewallEnabled(
  367. VARIANT_BOOL *pbEnabled )
  368. /*++
  369. CNetSharingConfiguration::GetInternetFirewallEnabled
  370. Routine Description:
  371. Arguments:
  372. none
  373. Return Value:
  374. none
  375. --*/
  376. {
  377. HNET_OEM_API_ENTER
  378. HRESULT hr = S_OK;
  379. if ( NULL == pbEnabled )
  380. {
  381. hr = E_POINTER;
  382. }
  383. else
  384. {
  385. BOOLEAN bEnabled = FALSE;
  386. hr = InternalGetFirewallEnabled( m_pHNetConnection, &bEnabled );
  387. *pbEnabled = bEnabled ? VARIANT_TRUE : VARIANT_FALSE;
  388. }
  389. return hr;
  390. HNET_OEM_API_LEAVE
  391. }
  392. STDMETHODIMP
  393. CNetSharingConfiguration::DisableInternetFirewall()
  394. /*++
  395. CNetSharingConfiguration::DisableInternetFirewall
  396. Routine Description:
  397. Arguments:
  398. none
  399. Return Value:
  400. none
  401. --*/
  402. {
  403. HNET_OEM_API_ENTER
  404. HRESULT hr;
  405. if ( !IsNotifyApproved() )
  406. {
  407. hr = E_ACCESSDENIED;
  408. }
  409. else
  410. {
  411. BOOLEAN bEnabled = FALSE;
  412. hr = InternalGetFirewallEnabled( m_pHNetConnection, &bEnabled );
  413. if ( SUCCEEDED(hr) && bEnabled )
  414. {
  415. IHNetFirewalledConnection* pFirewallConnection;
  416. hr = m_pHNetConnection->GetControlInterface(
  417. __uuidof(IHNetFirewalledConnection),
  418. reinterpret_cast<void**>(&pFirewallConnection) );
  419. if ( SUCCEEDED(hr) )
  420. {
  421. hr = pFirewallConnection->Unfirewall();
  422. ReleaseObj(pFirewallConnection);
  423. }
  424. }
  425. }
  426. return hr;
  427. HNET_OEM_API_LEAVE
  428. }
  429. STDMETHODIMP
  430. CNetSharingConfiguration::EnableInternetFirewall()
  431. /*++
  432. CNetSharingConfiguration::EnableInternetFirewall
  433. Routine Description:
  434. Arguments:
  435. none
  436. Return Value:
  437. none
  438. --*/
  439. {
  440. HNET_OEM_API_ENTER
  441. HRESULT hr;
  442. if ( !IsNotifyApproved() )
  443. {
  444. hr = E_ACCESSDENIED;
  445. }
  446. else
  447. {
  448. BOOLEAN bEnabled = FALSE;
  449. hr = InternalGetFirewallEnabled( m_pHNetConnection, &bEnabled );
  450. if ( SUCCEEDED(hr) && !bEnabled )
  451. {
  452. IHNetFirewalledConnection* pFirewalledConnection;
  453. hr = m_pHNetConnection->Firewall( &pFirewalledConnection );
  454. if ( SUCCEEDED(hr) )
  455. {
  456. ReleaseObj( pFirewalledConnection );
  457. }
  458. }
  459. }
  460. return hr;
  461. HNET_OEM_API_LEAVE
  462. }
  463. STDMETHODIMP
  464. CNetSharingConfiguration::AddPortMapping(
  465. OLECHAR* pszwName,
  466. UCHAR ucIPProtocol,
  467. USHORT usExternalPort,
  468. USHORT usInternalPort,
  469. DWORD dwOptions,
  470. OLECHAR* pszwTargetNameOrIPAddress,
  471. ICS_TARGETTYPE eTargetType,
  472. INetSharingPortMapping** ppMapping )
  473. /*++
  474. CNetSharingConfiguration::AddPortMapping
  475. Routine Description:
  476. Arguments:
  477. none
  478. Return Value:
  479. none
  480. --*/
  481. {
  482. HNET_OEM_API_ENTER
  483. OLECHAR* pszwTargetName = NULL;
  484. OLECHAR* pszwTargetIPAddress = NULL;
  485. if (eTargetType == ICSTT_NAME) pszwTargetName = pszwTargetNameOrIPAddress;
  486. else if (eTargetType == ICSTT_IPADDRESS) pszwTargetIPAddress = pszwTargetNameOrIPAddress;
  487. else
  488. return E_INVALIDARG;
  489. HRESULT hr = S_OK;
  490. if ( NULL == ppMapping )
  491. {
  492. hr = E_POINTER;
  493. }
  494. else if ( ( NULL == pszwName ) ||
  495. ( 0 == ucIPProtocol ) ||
  496. ( 0 == usInternalPort) || // should I allow this, implying internal == external?
  497. ( 0 == usExternalPort ) )
  498. {
  499. hr = E_INVALIDARG;
  500. }
  501. else if ( ( NULL == pszwTargetName ) &&
  502. ( NULL == pszwTargetIPAddress ) )
  503. {
  504. hr = E_INVALIDARG;
  505. }
  506. else if ( !IsSecureContext() )
  507. {
  508. hr = E_ACCESSDENIED;
  509. }
  510. else if ( NULL == m_pSettings )
  511. {
  512. hr = E_UNEXPECTED;
  513. }
  514. if ( SUCCEEDED(hr) )
  515. {
  516. *ppMapping = NULL;
  517. CComObject<CNetSharingPortMapping>* pNewMap;
  518. hr = CComObject<CNetSharingPortMapping>::CreateInstance( &pNewMap );
  519. if ( SUCCEEDED(hr) )
  520. {
  521. pNewMap->AddRef();
  522. IHNetPortMappingProtocol* pNewProtocol = NULL;
  523. // first, find existing IHNetPortMappingProtocol interface
  524. CComPtr<IEnumHNetPortMappingProtocols> spEnumProtocols = NULL;
  525. m_pSettings->EnumPortMappingProtocols (&spEnumProtocols);
  526. if (spEnumProtocols) {
  527. CComPtr<IHNetPortMappingProtocol> spHNetPortMappingProtocol = NULL;
  528. while (S_OK == spEnumProtocols->Next (1, &spHNetPortMappingProtocol, NULL)) {
  529. UCHAR ucProtocol = 0;
  530. spHNetPortMappingProtocol->GetIPProtocol (&ucProtocol);
  531. USHORT usPort = 0;
  532. spHNetPortMappingProtocol->GetPort (&usPort);
  533. if ((ucProtocol == ucIPProtocol) &&
  534. (ntohs(usPort) == usExternalPort)) {
  535. #if DBG
  536. OLECHAR * szwName = NULL;
  537. spHNetPortMappingProtocol->GetName (&szwName);
  538. if (szwName) {
  539. _ASSERT (!wcscmp (szwName, pszwName));
  540. CoTaskMemFree (szwName);
  541. }
  542. #endif
  543. pNewProtocol = spHNetPortMappingProtocol.Detach();
  544. break;
  545. }
  546. spHNetPortMappingProtocol = NULL;
  547. }
  548. }
  549. if (pNewProtocol == NULL) {
  550. hr = m_pSettings->CreatePortMappingProtocol(
  551. pszwName,
  552. ucIPProtocol,
  553. htons (usExternalPort),
  554. &pNewProtocol );
  555. }
  556. if ( SUCCEEDED(hr) )
  557. {
  558. IHNetPortMappingBinding *pNewBinding;
  559. hr = m_pHNetConnection->GetBindingForPortMappingProtocol(
  560. pNewProtocol,
  561. &pNewBinding );
  562. if ( SUCCEEDED(hr) )
  563. {
  564. if ( NULL == pszwTargetName )
  565. {
  566. ULONG ulAddress = IpPszToHostAddr( pszwTargetIPAddress );
  567. ulAddress = htonl (ulAddress);
  568. hr = pNewBinding->SetTargetComputerAddress( ulAddress );
  569. }
  570. else
  571. {
  572. hr = pNewBinding->SetTargetComputerName( pszwTargetName );
  573. }
  574. if ( SUCCEEDED(hr ) )
  575. {
  576. hr = pNewBinding->SetTargetPort (htons (usInternalPort));
  577. }
  578. if ( SUCCEEDED(hr ) )
  579. {
  580. pNewMap->Initialize( pNewBinding );
  581. *ppMapping = pNewMap;
  582. (*ppMapping)->AddRef();
  583. }
  584. ReleaseObj( pNewBinding );
  585. }
  586. ReleaseObj( pNewProtocol );
  587. }
  588. ReleaseObj(pNewMap);
  589. }
  590. }
  591. return hr;
  592. HNET_OEM_API_LEAVE
  593. }
  594. /*++
  595. CNetSharingConfiguration::RemovePortMapping
  596. Routine Description:
  597. Arguments:
  598. none
  599. Return Value:
  600. none
  601. --*/
  602. STDMETHODIMP
  603. CNetSharingConfiguration::RemovePortMapping(
  604. INetSharingPortMapping* pMapping
  605. )
  606. {
  607. HNET_OEM_API_ENTER
  608. HRESULT hr;
  609. if ( NULL == pMapping )
  610. {
  611. hr = E_INVALIDARG;
  612. }
  613. else if ( !IsSecureContext() )
  614. {
  615. hr = E_ACCESSDENIED;
  616. }
  617. else
  618. {
  619. hr = pMapping->Delete();
  620. }
  621. return hr;
  622. HNET_OEM_API_LEAVE
  623. }
  624. STDMETHODIMP
  625. CNetSharingPortMapping::get_Properties (INetSharingPortMappingProps ** ppNSPMP)
  626. // ICS_PORTMAPPING** ppProps)
  627. /*++
  628. CNetSharingPortMapping::get_Properties
  629. Routine Description:
  630. Arguments:
  631. ppProps
  632. Return Value:
  633. none
  634. --*/
  635. {
  636. // idea: use existing code to fill out ICS_PORTMAPPING,
  637. // then convert to INetSharingPortMappingProps
  638. HNET_OEM_API_ENTER
  639. ICS_PORTMAPPING * pProps = NULL;
  640. ICS_PORTMAPPING** ppProps = &pProps;
  641. HRESULT hr;
  642. #define Props (*ppProps)
  643. if ( NULL == ppNSPMP )
  644. {
  645. hr = E_POINTER;
  646. }
  647. else if ( !IsSecureContext() )
  648. {
  649. hr = E_ACCESSDENIED;
  650. }
  651. else
  652. {
  653. Props = reinterpret_cast<ICS_PORTMAPPING*>(CoTaskMemAlloc(sizeof(ICS_PORTMAPPING)));
  654. if ( NULL == Props )
  655. {
  656. hr = E_OUTOFMEMORY;
  657. }
  658. else
  659. {
  660. HRESULT hrName, hrAddr;
  661. ULONG ulAddress = 0L;
  662. ZeroMemory( Props, sizeof(ICS_PORTMAPPING) );
  663. hrName = _IProtocol()->GetTargetComputerName( &(Props->pszwTargetName) );
  664. hrAddr = _IProtocol()->GetTargetComputerAddress( &ulAddress );
  665. if ( SUCCEEDED(hrAddr) )
  666. {
  667. hrAddr = HostAddrToIpPsz( ulAddress, &(Props->pszwTargetIPAddress) );
  668. }
  669. if ( SUCCEEDED(hrName) || SUCCEEDED(hrAddr) )
  670. {
  671. IHNetPortMappingProtocol *pProtocol = NULL;
  672. hr = _IProtocol()->GetProtocol( &pProtocol );
  673. if ( SUCCEEDED(hr) )
  674. {
  675. hr = pProtocol->GetName( &(Props->pszwName) );
  676. if ( SUCCEEDED(hr) )
  677. {
  678. hr = pProtocol->GetIPProtocol( &(Props->ucIPProtocol) );
  679. }
  680. if ( SUCCEEDED(hr) )
  681. {
  682. hr = pProtocol->GetPort( &(Props->usExternalPort) );
  683. }
  684. ReleaseObj(pProtocol);
  685. }
  686. } else // actually, either error will do.
  687. hr = hrName; // ? hrName : hrAddr;
  688. if (SUCCEEDED(hr)) {
  689. // distinguish between TargetComputerName and TargetIPAddress
  690. if (Props->pszwTargetIPAddress && Props->pszwTargetName) {
  691. BOOLEAN fUseName;
  692. HRESULT hr1 = _IProtocol()->GetCurrentMethod (&fUseName);
  693. if (fUseName) {
  694. CoTaskMemFree( Props->pszwTargetIPAddress );
  695. Props->pszwTargetIPAddress = NULL;
  696. } else {
  697. CoTaskMemFree( Props->pszwTargetName );
  698. Props->pszwTargetName = NULL;
  699. }
  700. }
  701. // lastly, get enabled bit
  702. BOOLEAN b = FALSE;
  703. hr = _IProtocol()->GetEnabled (&b);
  704. Props->bEnabled = b == TRUE ? VARIANT_TRUE : VARIANT_FALSE;
  705. }
  706. if (SUCCEEDED(hr))
  707. {
  708. hr = _IProtocol()->GetTargetPort ( &(Props->usInternalPort) );
  709. }
  710. if ( FAILED(hr) )
  711. {
  712. if ( Props->pszwName )
  713. CoTaskMemFree( Props->pszwName );
  714. if ( Props->pszwTargetIPAddress )
  715. CoTaskMemFree( Props->pszwTargetIPAddress );
  716. if ( Props->pszwTargetName )
  717. CoTaskMemFree( Props->pszwTargetName );
  718. CoTaskMemFree( Props );
  719. Props = NULL;
  720. }
  721. }
  722. }
  723. if (Props) {
  724. // convert to INetSharingPortMappingProps **ppNSPMP
  725. CComObject<CNetSharingPortMappingProps>* pNSPMP = NULL;
  726. hr = CComObject<CNetSharingPortMappingProps>::CreateInstance (&pNSPMP);
  727. if (pNSPMP) {
  728. pNSPMP->AddRef();
  729. // adjust byte order
  730. Props->usExternalPort = ntohs (Props->usExternalPort);
  731. Props->usInternalPort = ntohs (Props->usInternalPort);
  732. hr = pNSPMP->SetRawData (Props);
  733. if (hr == S_OK)
  734. hr = pNSPMP->QueryInterface (__uuidof(INetSharingPortMappingProps), (void**)ppNSPMP);
  735. pNSPMP->Release();
  736. }
  737. if (Props->pszwName)
  738. CoTaskMemFree (Props->pszwName);
  739. if (Props->pszwTargetIPAddress)
  740. CoTaskMemFree (Props->pszwTargetIPAddress);
  741. if (Props->pszwTargetName)
  742. CoTaskMemFree (Props->pszwTargetName);
  743. CoTaskMemFree (Props);
  744. }
  745. #undef Props
  746. return hr;
  747. HNET_OEM_API_LEAVE
  748. }
  749. STDMETHODIMP
  750. CNetSharingPortMapping::Delete()
  751. /*++
  752. CNetSharingPortMapping::Delete
  753. Routine Description:
  754. Arguments:
  755. none
  756. Return Value:
  757. none
  758. --*/
  759. {
  760. HNET_OEM_API_ENTER
  761. HRESULT hr;
  762. Disable(); // if we can't delete it, at least disable it.
  763. // TODO: should I do this only if the Delete call below returns E_ACCESSDENIED?
  764. if ( _IProtocol() )
  765. {
  766. IHNetPortMappingProtocol* pPortMappingProtocol;
  767. hr = _IProtocol()->GetProtocol( &pPortMappingProtocol );
  768. if SUCCEEDED(hr)
  769. {
  770. pPortMappingProtocol->Delete();
  771. EnterCriticalSection( _CriticalSection() );
  772. _IProtocol( NULL );
  773. LeaveCriticalSection( _CriticalSection() );
  774. ReleaseObj( pPortMappingProtocol );
  775. }
  776. }
  777. else
  778. {
  779. hr = E_UNEXPECTED;
  780. }
  781. return hr;
  782. HNET_OEM_API_LEAVE
  783. }