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.

975 lines
24 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001
  5. //
  6. // File : CNATStaticPortMappingService.cpp
  7. //
  8. // Contents : CNATStaticPortMappingService implementation
  9. //
  10. // Notes :
  11. //
  12. // Author : savasg 28 February 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "CNATStaticPortMappingService.h"
  18. #include "ipnat.h"
  19. #include "winsock2.h"
  20. #include "debug.h"
  21. //
  22. // GLOBALS
  23. //
  24. IHNetIcsSettings* g_IcsSettingsp = NULL;
  25. HRESULT
  26. SeekPortMapping(
  27. IN OPTIONAL LPOLESTR searchNamep,
  28. IN OPTIONAL USHORT searchPort,
  29. OUT IHNetPortMappingProtocol **Protocolpp
  30. )
  31. //
  32. // Seeks and retrieves a MappingProtocol by Name or Port
  33. //
  34. {
  35. HRESULT hr = S_OK;
  36. IHNetProtocolSettings* ProtocolSettingsp = NULL;
  37. IEnumHNetPortMappingProtocols* EnumProtocolsp = NULL;
  38. IHNetPortMappingProtocol* Protocolp = NULL;
  39. LPOLESTR ProtocolNamep = NULL;
  40. USHORT ProtocolPort = 0;
  41. BOOLEAN bFound = FALSE;
  42. DBG_SPEW(TM_STATIC, TL_ERROR, L" > SeekPortMapping \n");
  43. do
  44. {
  45. hr = g_IcsSettingsp->QueryInterface(IID_IHNetProtocolSettings,
  46. reinterpret_cast<void**>(&ProtocolSettingsp));
  47. if( FAILED(hr) )
  48. {
  49. DBG_SPEW(TM_STATIC, TL_ERROR, L"Query Interface failed for ProtocolSettingsp e:%X", hr);
  50. break;
  51. }
  52. hr = ProtocolSettingsp->EnumPortMappingProtocols(&EnumProtocolsp);
  53. if ( FAILED(hr) )
  54. {
  55. DBG_SPEW(TM_STATIC, TL_ERROR, L"Enum Interface can't be retrieved \n");
  56. break;
  57. }
  58. while(
  59. (FALSE == bFound) &&
  60. (S_OK == EnumProtocolsp->Next(1, &Protocolp, NULL))
  61. )
  62. {
  63. hr = Protocolp->GetName(&ProtocolNamep);
  64. if( FAILED(hr) )
  65. {
  66. DBG_SPEW(TM_STATIC, TL_ERROR, L"problemo name\n");
  67. }
  68. hr = Protocolp->GetPort(&ProtocolPort);
  69. if( FAILED(hr) )
  70. {
  71. DBG_SPEW(TM_STATIC, TL_ERROR, L" problemo ");
  72. break;
  73. }
  74. if(searchNamep &&
  75. !( wcscmp(ProtocolNamep, searchNamep) )
  76. )
  77. {
  78. DBG_SPEW(TM_STATIC, TL_ERROR, L"The Name is hit %S\n", searchNamep);
  79. bFound = TRUE;
  80. }
  81. if(searchPort &&
  82. (searchPort == ProtocolPort))
  83. {
  84. bFound = TRUE;
  85. }
  86. CoTaskMemFree(ProtocolNamep);
  87. ProtocolNamep = NULL;
  88. if (FALSE == bFound) Protocolp->Release();
  89. }
  90. EnumProtocolsp->Release();
  91. } while ( FALSE );
  92. if(ProtocolSettingsp != NULL)
  93. {
  94. ProtocolSettingsp->Release();
  95. }
  96. if(Protocolpp && (bFound == TRUE) )
  97. {
  98. *Protocolpp = Protocolp;
  99. }
  100. return hr;
  101. }
  102. HRESULT
  103. DeleteMappingByName(LPOLESTR ProtocolNamep)
  104. {
  105. HRESULT hr = S_OK;
  106. IHNetPortMappingProtocol* Protocolp = NULL;
  107. DBG_SPEW(TM_STATIC, TL_ERROR, L" > DeleteMappingByName \n");
  108. do
  109. {
  110. if(ProtocolNamep == NULL)
  111. {
  112. break;
  113. }
  114. hr = SeekPortMapping(ProtocolNamep,
  115. 0,
  116. &Protocolp);
  117. if( FAILED(hr) ||
  118. (Protocolp == NULL))
  119. {
  120. DBG_SPEW(TM_STATIC, TL_ERROR, L"No Such Protocol %S: %X", ProtocolNamep, hr);
  121. break;
  122. }
  123. DBG_SPEW(TM_STATIC, TL_ERROR, L"DELETING\n");
  124. hr = Protocolp->Delete();
  125. Protocolp->Release();
  126. } while ( FALSE );
  127. return hr;
  128. }
  129. CNATStaticPortMappingService::CNATStaticPortMappingService()
  130. {
  131. m_pEventSink = NULL;
  132. m_pHNetConnection = NULL;
  133. }
  134. HRESULT CNATStaticPortMappingService::FinalConstruct()
  135. {
  136. HRESULT hr = S_OK;
  137. return hr;
  138. }
  139. HRESULT CNATStaticPortMappingService::FinalRelease()
  140. {
  141. HRESULT hr = S_OK;
  142. if(NULL != m_pHNetConnection)
  143. {
  144. m_pHNetConnection->Release();
  145. }
  146. return hr;
  147. }
  148. HRESULT
  149. CNATStaticPortMappingService::Initialize(IHNetConnection* pHNetConnection)
  150. /*++
  151. Routine Description:
  152. Arguments:
  153. none
  154. Return Value:
  155. none
  156. --*/
  157. {
  158. HRESULT hr = S_OK;
  159. IHNetConnection* HomenetConnectionp = NULL;
  160. IEnumHNetIcsPublicConnections* EnumIcsPublicConnectionsp = NULL;
  161. IHNetIcsPublicConnection* PublicConnectionp = NULL;
  162. DBG_SPEW(TM_STATIC, TL_ERROR, L" > Initialize \n");
  163. do
  164. {
  165. hr = CoCreateInstance(CLSID_HNetCfgMgr,
  166. NULL,
  167. CLSCTX_SERVER,
  168. IID_IHNetIcsSettings,
  169. reinterpret_cast<void**>(&g_IcsSettingsp));
  170. if( FAILED(hr) )
  171. {
  172. DBG_SPEW(TM_STATIC, TL_ERROR, L" CoCreateInstance for IID_IHNetIcsSettings failed \n");
  173. break;
  174. }
  175. //
  176. // Get the Enumeration Interface
  177. //
  178. hr = g_IcsSettingsp->EnumIcsPublicConnections(&EnumIcsPublicConnectionsp);
  179. if( FAILED(hr) )
  180. {
  181. DBG_SPEW(TM_STATIC, TL_ERROR, L"Getting Interface for Enumeration of Public Connections has failed \n");
  182. break;
  183. }
  184. //
  185. // Find the Interface .. for now there is only one Public Interface
  186. // This is not the RRAS case
  187. //
  188. hr = EnumIcsPublicConnectionsp->Next(1,
  189. &PublicConnectionp,
  190. NULL);
  191. if( FAILED(hr) )
  192. {
  193. DBG_SPEW(TM_STATIC, TL_ERROR, L"There is no Public Connection.. how come?\n");
  194. break;
  195. }
  196. hr = PublicConnectionp->QueryInterface(IID_IHNetConnection,
  197. reinterpret_cast<void**>(&HomenetConnectionp));
  198. _ASSERT( SUCCEEDED(hr) );
  199. if( FAILED(hr) )
  200. {
  201. EnumIcsPublicConnectionsp->Release();
  202. PublicConnectionp->Release();
  203. DBG_SPEW(TM_STATIC, TL_ERROR, L"Can't Get the IID_IHNetConnection Interface from the Public Connection\n");
  204. break;
  205. }
  206. } while( FALSE );
  207. //
  208. // Release the Ref counts
  209. //
  210. if(PublicConnectionp != NULL)
  211. {
  212. PublicConnectionp->Release();
  213. }
  214. if(EnumIcsPublicConnectionsp != NULL)
  215. {
  216. EnumIcsPublicConnectionsp->Release();
  217. }
  218. m_pHNetConnection = HomenetConnectionp;
  219. /* Instead of Using the existing
  220. m_pHNetConnection = pHNetConnection;
  221. m_pHNetConnection->AddRef();
  222. */
  223. return hr;
  224. }
  225. HRESULT CNATStaticPortMappingService::Advise(IUPnPEventSink* pesSubscriber)
  226. {
  227. HRESULT hr = S_OK;
  228. m_pEventSink = pesSubscriber;
  229. m_pEventSink->AddRef();
  230. return hr;
  231. }
  232. HRESULT CNATStaticPortMappingService::Unadvise(IUPnPEventSink* pesSubscriber)
  233. {
  234. HRESULT hr = S_OK;
  235. m_pEventSink->Release();
  236. m_pEventSink = NULL;
  237. return hr;
  238. }
  239. HRESULT CNATStaticPortMappingService::get_StaticPortDescriptionList(BSTR* pStaticPortDescriptionList)
  240. {
  241. HRESULT hr = S_OK;
  242. *pStaticPortDescriptionList = NULL;
  243. typedef struct tagNameEnablePair
  244. {
  245. LIST_ENTRY LinkField;
  246. LPOLESTR pName;
  247. ULONG ulNameSize;
  248. BOOLEAN bEnabled;
  249. } NameEnablePair;
  250. LIST_ENTRY PairList;
  251. InitializeListHead(&PairList);
  252. // First construct a linklist of NameEnablePairs with the info needed
  253. IEnumHNetPortMappingBindings *pBindingEnum = NULL;
  254. hr = m_pHNetConnection->EnumPortMappings(FALSE, &pBindingEnum);
  255. if(SUCCEEDED(hr))
  256. {
  257. IHNetPortMappingBinding* pBinding;
  258. while(S_OK == pBindingEnum->Next(1, &pBinding, NULL))
  259. {
  260. BOOLEAN bEnabled;
  261. hr = pBinding->GetEnabled(&bEnabled);
  262. if(SUCCEEDED(hr))
  263. {
  264. IHNetPortMappingProtocol* pProtocol;
  265. hr = pBinding->GetProtocol(&pProtocol);
  266. if(SUCCEEDED(hr))
  267. {
  268. LPOLESTR pName;
  269. hr = pProtocol->GetName(&pName);
  270. if(SUCCEEDED(hr))
  271. {
  272. NameEnablePair* pPair = new NameEnablePair;
  273. if(NULL != pPair)
  274. {
  275. pPair->pName = pName;
  276. pPair->bEnabled = bEnabled;
  277. InsertTailList(&PairList, &pPair->LinkField);
  278. }
  279. //CoTaskMemFree(pName);
  280. }
  281. pProtocol->Release();
  282. }
  283. }
  284. pBinding->Release();
  285. }
  286. pBindingEnum->Release();
  287. }
  288. LIST_ENTRY* pCount;
  289. NameEnablePair* pContainingPair;
  290. if(SUCCEEDED(hr))
  291. {
  292. // Count the space needed in the return string
  293. unsigned int uSizeNeeded = 0;
  294. pCount = PairList.Flink;
  295. while(&PairList != pCount)
  296. {
  297. pContainingPair = CONTAINING_RECORD(pCount, NameEnablePair, LinkField);
  298. pContainingPair->ulNameSize = lstrlen(pContainingPair->pName);
  299. uSizeNeeded += 3 + pContainingPair->ulNameSize; //Name:1,
  300. pCount = pCount->Flink;
  301. }
  302. BSTR pReturnString;
  303. if(0 != uSizeNeeded)
  304. {
  305. pReturnString = SysAllocStringLen(NULL, uSizeNeeded);
  306. }
  307. else
  308. {
  309. pReturnString = SysAllocString(L"");
  310. }
  311. if(NULL != pReturnString)
  312. {
  313. *pStaticPortDescriptionList = pReturnString;
  314. // Fill in the string
  315. pCount = PairList.Flink;
  316. while(&PairList != pCount)
  317. {
  318. pContainingPair = CONTAINING_RECORD(pCount, NameEnablePair, LinkField);
  319. lstrcpy(pReturnString, pContainingPair->pName);
  320. pReturnString += pContainingPair->ulNameSize;
  321. *pReturnString = L':';
  322. pReturnString++;
  323. *pReturnString = pContainingPair->bEnabled ? L'1' : L'0';
  324. pReturnString++;
  325. *pReturnString = (&PairList == pCount->Flink) ? L'\0' : L',';
  326. pReturnString++;
  327. pCount = pCount->Flink;
  328. }
  329. }
  330. else
  331. {
  332. hr = E_OUTOFMEMORY;
  333. }
  334. }
  335. // Clean up the linked list
  336. pCount = PairList.Flink;
  337. while(&PairList != pCount)
  338. {
  339. NameEnablePair* pDelete = CONTAINING_RECORD(pCount, NameEnablePair, LinkField);
  340. pCount = pCount->Flink;
  341. CoTaskMemFree(pDelete->pName);
  342. delete pDelete;
  343. }
  344. return hr;
  345. }
  346. HRESULT CNATStaticPortMappingService::get_StaticPort(ULONG* pulStaticPort)
  347. {
  348. return E_UNEXPECTED;
  349. }
  350. HRESULT CNATStaticPortMappingService::get_StaticPortProtocol(BSTR* pStaticPortProtocol)
  351. {
  352. *pStaticPortProtocol = NULL;
  353. return E_UNEXPECTED;
  354. }
  355. HRESULT CNATStaticPortMappingService::get_StaticPortClient(BSTR* pStaticPortClient)
  356. {
  357. *pStaticPortClient = NULL;
  358. return E_UNEXPECTED;
  359. }
  360. HRESULT CNATStaticPortMappingService::get_StaticPortEnable(VARIANT_BOOL* pbStaticPortEnable)
  361. {
  362. return E_UNEXPECTED;
  363. }
  364. HRESULT CNATStaticPortMappingService::get_StaticPortDescription(BSTR* pStaticPortDescription)
  365. {
  366. *pStaticPortDescription = NULL;
  367. return E_UNEXPECTED;
  368. }
  369. HRESULT CNATStaticPortMappingService::GetStaticPortMappingList(BSTR* pStaticPortMappingList)
  370. {
  371. HRESULT hr = S_OK;
  372. SysFreeString(*pStaticPortMappingList);
  373. *pStaticPortMappingList = NULL;
  374. hr = get_StaticPortDescriptionList(pStaticPortMappingList);
  375. return hr;
  376. }
  377. HRESULT
  378. CNATStaticPortMappingService::GetStaticPortMapping(
  379. BSTR StaticPortMappingDescription,
  380. ULONG* pulStaticPort,
  381. BSTR* pStaticPortClient,
  382. BSTR* pStaticPortProtocol
  383. )
  384. /*++
  385. Routine Description:
  386. Retrieves the Port, Client Name/Address and the Protocol for a given
  387. Mapping (redirect in NAT context)... Note that a Binding may not exist
  388. for a Mapping.. Thus There might not be a Client Name/Address.
  389. Arguments:
  390. none
  391. Return Value:
  392. none
  393. --*/
  394. {
  395. HRESULT hr = S_OK;
  396. IHNetPortMappingProtocol* Protocolp = NULL;
  397. IHNetPortMappingBinding* Bindingp = NULL;
  398. LPOLESTR ClientNamep = NULL;
  399. ULONG ClientAddress = 0;
  400. USHORT ProtocolPort = 0;
  401. UCHAR ProtocolType = 0;
  402. DBG_SPEW(TM_STATIC, TL_ERROR, L" > GetStaticPortMapping");
  403. _ASSERT( StaticPortMappingDescription != NULL );
  404. _ASSERT( pulStaticPort != NULL );
  405. _ASSERT( pStaticPortClient != NULL );
  406. _ASSERT( pStaticPortProtocol != NULL );
  407. SysFreeString(*pStaticPortClient);
  408. SysFreeString(*pStaticPortProtocol);
  409. *pStaticPortClient = NULL;
  410. *pStaticPortProtocol = NULL;
  411. do
  412. {
  413. hr = SeekPortMapping((LPOLESTR)StaticPortMappingDescription,
  414. 0,
  415. &Protocolp);
  416. if( FAILED(hr) )
  417. {
  418. DBG_SPEW(TM_STATIC, TL_ERROR, L"There is no such Port Mapping");
  419. break;
  420. }
  421. hr = Protocolp->GetPort(&ProtocolPort); //USHORT
  422. _ASSERT( SUCCEEDED(hr) );
  423. *pulStaticPort = ProtocolPort;
  424. //
  425. // Get the Type of the Protocol Mapping and put the appropriate
  426. // String
  427. //
  428. hr = Protocolp->GetIPProtocol(&ProtocolType); //UCHAR
  429. _ASSERT( SUCCEEDED(hr) );
  430. if ( ProtocolType == NAT_PROTOCOL_TCP )
  431. {
  432. *pStaticPortProtocol = SysAllocString(L"TCP");
  433. }
  434. else if ( ProtocolType == NAT_PROTOCOL_UDP )
  435. {
  436. *pStaticPortProtocol = SysAllocString(L"UDP");
  437. }
  438. else
  439. {
  440. _ASSERT( FALSE );
  441. }
  442. //
  443. // A Binding may not exist.. That's Ok..
  444. //
  445. hr = m_pHNetConnection->GetBindingForPortMappingProtocol(Protocolp, &Bindingp);
  446. if ( FAILED(hr) )
  447. {
  448. DBG_SPEW(TM_STATIC, TL_ERROR, L" No Such Binding for that protocol.");
  449. hr = S_OK;
  450. break;
  451. }
  452. //
  453. // If the Address exist just process that.
  454. // Convert the Name to a OLESTR
  455. //
  456. hr = Bindingp->GetTargetComputerAddress(&ClientAddress);
  457. if ( SUCCEEDED(hr) )
  458. {
  459. ClientNamep = (LPOLESTR) CoTaskMemAlloc( (wcslen(INET_NTOW(ClientAddress)) + 1) * sizeof(WCHAR) );
  460. _ASSERT(ClientNamep != NULL);
  461. wcscpy( ClientNamep, INET_NTOW(ClientAddress) );
  462. }
  463. else
  464. {
  465. hr = Bindingp->GetTargetComputerName(&ClientNamep);
  466. }
  467. if( FAILED( hr ) || (ClientNamep == NULL))
  468. {
  469. DBG_SPEW(TM_STATIC, TL_ERROR, L"Can't Retrieve Name or Address of Client from Binding ");
  470. break;
  471. }
  472. *pStaticPortClient = SysAllocString( ClientNamep );
  473. if (*pStaticPortClient == NULL)
  474. {
  475. hr = E_OUTOFMEMORY;
  476. }
  477. } while ( FALSE );
  478. if ( ClientNamep != NULL)
  479. {
  480. CoTaskMemFree( ClientNamep );
  481. }
  482. if ( Bindingp != NULL)
  483. {
  484. Bindingp->Release();
  485. }
  486. if ( Protocolp != NULL )
  487. {
  488. Protocolp->Release();
  489. }
  490. //
  491. // If there was a failure then clear up the allocated Strings
  492. //
  493. if ( FAILED(hr) && ( *pStaticPortProtocol != NULL ) )
  494. {
  495. SysFreeString( *pStaticPortProtocol );
  496. *pStaticPortProtocol = NULL;
  497. }
  498. return hr;
  499. }
  500. HRESULT
  501. CNATStaticPortMappingService::SetStaticPortMappingEnabled(
  502. BSTR StaticPortMappingDescription,
  503. VARIANT_BOOL bStaticPortEnable
  504. )
  505. /*++
  506. Routine Description:
  507. Arguments:
  508. none
  509. Return Value:
  510. none
  511. --*/
  512. {
  513. HRESULT hr = S_OK;
  514. IHNetPortMappingProtocol* Protocolp = NULL;
  515. IHNetPortMappingBinding* Bindingp = NULL;
  516. DBG_SPEW(TM_STATIC, TL_ERROR, L" > SetStaticPortMappingEnabled");
  517. do
  518. {
  519. hr = SeekPortMapping((LPOLESTR)StaticPortMappingDescription,
  520. NULL,
  521. &Protocolp);
  522. if( FAILED(hr) )
  523. {
  524. DBG_SPEW(TM_STATIC, TL_ERROR, L"There is no such Port Mapping");
  525. break;
  526. }
  527. hr = m_pHNetConnection->GetBindingForPortMappingProtocol(Protocolp, &Bindingp);
  528. if ( FAILED(hr) )
  529. {
  530. DBG_SPEW(TM_STATIC, TL_ERROR, L" No Such Binding for that protocol.");
  531. break;
  532. }
  533. if ( VARIANT_TRUE == bStaticPortEnable )
  534. {
  535. hr = Bindingp->SetEnabled(TRUE);
  536. }
  537. else if ( VARIANT_FALSE == bStaticPortEnable )
  538. {
  539. hr = Bindingp->SetEnabled(FALSE);
  540. }
  541. _ASSERT( SUCCEEDED(hr) );
  542. } while ( FALSE );
  543. if (Protocolp != NULL)
  544. {
  545. Protocolp->Release();
  546. }
  547. if (Bindingp != NULL)
  548. {
  549. Bindingp->Release();
  550. }
  551. return hr;
  552. }
  553. HRESULT
  554. CNATStaticPortMappingService::CreateStaticPortMapping(
  555. BSTR StaticPortMappingDescription,
  556. ULONG ulStaticPort,
  557. BSTR StaticPortClient,
  558. BSTR StaticPortProtocol
  559. )
  560. /*++
  561. Routine Description:
  562. Arguments:
  563. none
  564. Return Value:
  565. none
  566. --*/
  567. {
  568. HRESULT hr = S_OK;
  569. UCHAR ProtocolType = 0;
  570. IHNetProtocolSettings* ProtocolSettingsp = NULL;
  571. IHNetPortMappingProtocol* PortMappingProtocolp = NULL;
  572. IHNetPortMappingBinding* PortMappingBindingp = NULL;
  573. ULONG ClientAddr = 0;
  574. ASSERT( StaticPortMappingDescription != NULL );
  575. ASSERT( ulStaticPort == 0 );
  576. // ASSERT( StaticPortClient != NULL );
  577. ASSERT( StaticPortProtocol );
  578. DBG_SPEW(TM_STATIC, TL_ERROR, L" > CreateStaticPortMapping");
  579. //
  580. // Check and transform the Protocol Value to its correct type.
  581. //
  582. if( wcscmp(StaticPortProtocol, L"TCP") == 0)
  583. {
  584. ProtocolType = NAT_PROTOCOL_TCP;
  585. }
  586. else if ( wcscmp(StaticPortProtocol, L"UDP") == 0)
  587. {
  588. ProtocolType = NAT_PROTOCOL_UDP;
  589. }
  590. else
  591. {
  592. DBG_SPEW(TM_STATIC, TL_ERROR, L"Unknown Protocol Type\n");
  593. _ASSERT(FALSE);
  594. return E_INVALIDARG;
  595. }
  596. do
  597. {
  598. hr = g_IcsSettingsp->QueryInterface(IID_IHNetProtocolSettings,
  599. reinterpret_cast<void**>(&ProtocolSettingsp));
  600. _ASSERT( SUCCEEDED(hr) );
  601. if( FAILED(hr) )
  602. {
  603. break;
  604. }
  605. hr = ProtocolSettingsp->CreatePortMappingProtocol((LPOLESTR)StaticPortMappingDescription,
  606. ProtocolType,
  607. (USHORT)ulStaticPort,
  608. &PortMappingProtocolp);
  609. if( FAILED(hr) )
  610. {
  611. DBG_SPEW(TM_STATIC, TL_ERROR, L"Creating the PortMapping has failed");
  612. break;
  613. }
  614. //
  615. // If there is no
  616. //
  617. if ( StaticPortClient != NULL )
  618. {
  619. hr = m_pHNetConnection->GetBindingForPortMappingProtocol(PortMappingProtocolp,
  620. &PortMappingBindingp);
  621. if( FAILED(hr) )
  622. {
  623. DBG_SPEW(TM_STATIC, TL_ERROR, L" GetBinding for PORT Mapping has failed ");
  624. break;
  625. }
  626. //
  627. // Decide wether the given address is a Name or an valid IP address
  628. // ient_addrw will return an INADDR_NONE if the address is not
  629. // decimal doted IP address
  630. //
  631. ClientAddr = INET_ADDR((LPOLESTR)StaticPortClient);
  632. if( ClientAddr == INADDR_NONE)
  633. {
  634. hr = PortMappingBindingp->SetTargetComputerName(StaticPortClient);
  635. _ASSERT( SUCCEEDED(hr) );
  636. }
  637. else
  638. {
  639. hr = PortMappingBindingp->SetTargetComputerAddress(ClientAddr);
  640. _ASSERT( SUCCEEDED(hr) );
  641. }
  642. //
  643. // It creates it enabled
  644. //
  645. hr = PortMappingBindingp->SetEnabled(TRUE);
  646. }
  647. } while (FALSE);
  648. if( PortMappingProtocolp != NULL)
  649. {
  650. PortMappingProtocolp->Release();
  651. }
  652. if( ProtocolSettingsp != NULL)
  653. {
  654. ProtocolSettingsp->Release();
  655. }
  656. return hr;
  657. }
  658. HRESULT
  659. CNATStaticPortMappingService::DeleteStaticPortMapping(BSTR StaticPortMappingDescription)
  660. /*++
  661. Routine Description:
  662. Arguments:
  663. none
  664. Return Value:
  665. none
  666. --*/
  667. {
  668. HRESULT hr = S_OK;
  669. DBG_SPEW(TM_STATIC, TL_ERROR, L"> DeleteStaticPortMapping");
  670. _ASSERT( StaticPortMappingDescription != NULL );
  671. hr = DeleteMappingByName( (LPOLESTR) StaticPortMappingDescription );
  672. return hr;
  673. }
  674. HRESULT
  675. CNATStaticPortMappingService::SetStaticPortMapping(
  676. BSTR StaticPortMappingDescription,
  677. ULONG ulStaticPort,
  678. BSTR StaticPortClient,
  679. BSTR StaticPortProtocol
  680. )
  681. /*++
  682. Routine Description:
  683. Arguments:
  684. none
  685. Return Value:
  686. none
  687. --*/
  688. {
  689. HRESULT hr = S_OK;
  690. hr = DeleteMappingByName((LPOLESTR) StaticPortMappingDescription);
  691. if ( SUCCEEDED(hr) )
  692. {
  693. hr = this->CreateStaticPortMapping(StaticPortMappingDescription,
  694. ulStaticPort,
  695. StaticPortClient,
  696. StaticPortProtocol);
  697. _ASSERT( SUCCEEDED(hr) );
  698. }
  699. return hr;
  700. }