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.

144 lines
3.7 KiB

  1. // SPrtMapC.cpp : Implementation of CStaticPortMappingCollection
  2. #include "stdafx.h"
  3. #pragma hdrstop
  4. #include "NATUPnP.h"
  5. #include "SPrtMapC.h"
  6. #include "SPortMap.h"
  7. static HRESULT CreateDynamicCollection (IUPnPService * pUPS, IDynamicPortMappingCollection ** ppDPMC)
  8. {
  9. CComObject<CDynamicPortMappingCollection> * pDPMC = NULL;
  10. HRESULT hr = CComObject<CDynamicPortMappingCollection>::CreateInstance (&pDPMC);
  11. if (pDPMC) {
  12. pDPMC->AddRef();
  13. // init
  14. hr = pDPMC->Initialize (pUPS);
  15. if (SUCCEEDED(hr))
  16. hr = pDPMC->QueryInterface (__uuidof(IDynamicPortMappingCollection), (void**)ppDPMC);
  17. pDPMC->Release();
  18. }
  19. return hr;
  20. }
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CStaticPortMappingCollection
  23. STDMETHODIMP CStaticPortMappingCollection::get_Item(long lExternalPort, BSTR bstrProtocol, IStaticPortMapping ** ppSPM)
  24. {
  25. NAT_API_ENTER
  26. if (!ppSPM)
  27. return E_POINTER;
  28. *ppSPM = NULL;
  29. CComPtr<IDynamicPortMappingCollection> spDPMC = NULL;
  30. HRESULT hr = CreateDynamicCollection (m_spUPS, &spDPMC);
  31. if (spDPMC) {
  32. CComPtr<IDynamicPortMapping> spDPM = NULL;
  33. hr = spDPMC->get_Item (L"", lExternalPort, bstrProtocol, &spDPM);
  34. if (spDPM) {
  35. *ppSPM = CStaticPortMapping::CreateInstance (spDPM);
  36. if (!*ppSPM)
  37. hr = E_OUTOFMEMORY;
  38. }
  39. }
  40. return hr;
  41. NAT_API_LEAVE
  42. }
  43. STDMETHODIMP CStaticPortMappingCollection::get_Count(long *pVal)
  44. {
  45. NAT_API_ENTER
  46. if (!pVal)
  47. return E_POINTER;
  48. *pVal = 0;
  49. long lCount = 0;
  50. CComPtr<IUnknown> spUnk = NULL;
  51. HRESULT hr = get__NewEnum (&spUnk);
  52. if (spUnk) {
  53. CComPtr<IEnumVARIANT> spEV = NULL;
  54. hr = spUnk->QueryInterface (__uuidof(IEnumVARIANT), (void**)&spEV);
  55. if (spEV) {
  56. spEV->Reset();
  57. CComVariant cv;
  58. while (S_OK == spEV->Next (1, &cv, NULL)) {
  59. lCount++;
  60. cv.Clear();
  61. }
  62. }
  63. }
  64. *pVal = lCount;
  65. return hr;
  66. NAT_API_LEAVE
  67. }
  68. STDMETHODIMP CStaticPortMappingCollection::Remove(long lExternalPort, BSTR bstrProtocol)
  69. {
  70. NAT_API_ENTER
  71. CComPtr<IDynamicPortMappingCollection> spDPMC = NULL;
  72. HRESULT hr = CreateDynamicCollection (m_spUPS, &spDPMC);
  73. if (spDPMC)
  74. hr = spDPMC->Remove (L"", lExternalPort, bstrProtocol);
  75. return hr;
  76. NAT_API_LEAVE
  77. }
  78. STDMETHODIMP CStaticPortMappingCollection::Add(long lExternalPort, BSTR bstrProtocol, long lInternalPort, BSTR bstrInternalClient, VARIANT_BOOL bEnabled, BSTR bstrDescription, IStaticPortMapping ** ppSPM)
  79. {
  80. NAT_API_ENTER
  81. if (!ppSPM)
  82. return E_POINTER;
  83. *ppSPM = NULL;
  84. CComPtr<IDynamicPortMappingCollection> spDPMC = NULL;
  85. HRESULT hr = CreateDynamicCollection (m_spUPS, &spDPMC);
  86. if (spDPMC) {
  87. CComPtr<IDynamicPortMapping> spDPM = NULL;
  88. hr = spDPMC->Add (L"", lExternalPort, bstrProtocol, lInternalPort, bstrInternalClient, bEnabled, bstrDescription, 0L, &spDPM);
  89. if (spDPM) {
  90. *ppSPM = CStaticPortMapping::CreateInstance (spDPM);
  91. if (!*ppSPM)
  92. hr = E_OUTOFMEMORY;
  93. }
  94. }
  95. return hr;
  96. NAT_API_LEAVE
  97. }
  98. STDMETHODIMP CStaticPortMappingCollection::get__NewEnum(IUnknown **ppVal)
  99. {
  100. NAT_API_ENTER
  101. if (!ppVal)
  102. return E_POINTER;
  103. *ppVal = NULL;
  104. CComPtr<IEnumVARIANT> spEV =
  105. CEnumStaticPortMappingCollection::CreateInstance (m_spUPS);
  106. if (!spEV)
  107. return E_OUTOFMEMORY;
  108. return spEV->QueryInterface (__uuidof(IUnknown), (void**)ppVal);
  109. NAT_API_LEAVE
  110. }
  111. HRESULT CStaticPortMappingCollection::Initialize (IUPnPService * pUPS)
  112. {
  113. _ASSERT (pUPS);
  114. _ASSERT (m_spUPS == NULL);
  115. m_spUPS = pUPS;
  116. return S_OK;
  117. }