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.

153 lines
3.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: I N T E R F A C E H E L P E R . C P P
  7. //
  8. // Contents: Manages dealing with multiple interfaces and interface changes
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 6 Feb 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "InterfaceHelper.h"
  18. #include "ssdp.h"
  19. #include <iphlpapi.h>
  20. #include <winsock2.h>
  21. #include "cache.h"
  22. CInterfaceHelper CInterfaceHelper::s_instance;
  23. CInterfaceHelper::CInterfaceHelper()
  24. {
  25. }
  26. CInterfaceHelper::~CInterfaceHelper()
  27. {
  28. }
  29. CInterfaceHelper & CInterfaceHelper::Instance()
  30. {
  31. return s_instance;
  32. }
  33. void CInterfaceHelper::OnInterfaceChange(const InterfaceMappingList & interfaceMappingList)
  34. {
  35. HRESULT hr = S_OK;
  36. InterfaceList interfaceList;
  37. {
  38. CLock lock(m_critSec);
  39. hr = HrGenerateDirtyGuidList(m_interfaceMappingList, interfaceMappingList, interfaceList);
  40. if(SUCCEEDED(hr))
  41. {
  42. // Copy the interface mapping list
  43. long nCount = interfaceMappingList.GetCount();
  44. m_interfaceMappingList.Clear();
  45. if (nCount)
  46. {
  47. hr = m_interfaceMappingList.HrSetCount(nCount);
  48. if(SUCCEEDED(hr))
  49. {
  50. for(long n = 0; n < nCount; ++n)
  51. {
  52. m_interfaceMappingList[n] = interfaceMappingList[n];
  53. }
  54. }
  55. }
  56. }
  57. }
  58. // Do this outside of lock
  59. if(SUCCEEDED(hr))
  60. {
  61. hr = CSsdpCacheEntryManager::Instance().HrClearDirtyInterfaceGuids(interfaceList.GetCount(), interfaceList.GetData());
  62. }
  63. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceHelper::OnInterfaceChange");
  64. }
  65. HRESULT CInterfaceHelper::HrInitialize()
  66. {
  67. CLock lock(m_critSec);
  68. HRESULT hr = S_OK;
  69. hr = CUPnPInterfaceList::Instance().HrRegisterInterfaceChange(this);
  70. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceHelper::HrInitialize");
  71. return hr;
  72. }
  73. HRESULT CInterfaceHelper::HrShutdown()
  74. {
  75. HRESULT hr = S_OK;
  76. CLock lock(m_critSec);
  77. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceHelper::HrShutdown");
  78. return hr;
  79. }
  80. HRESULT CInterfaceHelper::HrResolveAddress(DWORD dwIpAddress, GUID & guidInterface)
  81. {
  82. CLock lock(m_critSec);
  83. HRESULT hr = S_OK;
  84. ZeroMemory(&guidInterface, sizeof(guidInterface));
  85. long nCount = m_interfaceMappingList.GetCount();
  86. for(long n = 0; n < nCount; ++n)
  87. {
  88. if(m_interfaceMappingList[n].m_dwIpAddress == dwIpAddress)
  89. {
  90. guidInterface = m_interfaceMappingList[n].m_guidInterface;
  91. break;
  92. }
  93. }
  94. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceHelper::HrResolveAddress");
  95. return hr;
  96. }
  97. HRESULT CInterfaceHelper::HrGenerateDirtyGuidList(
  98. const InterfaceMappingList & interfaceMappingListOld,
  99. const InterfaceMappingList & interfaceMappingListNew,
  100. InterfaceList & interfaceList)
  101. {
  102. CLock lock(m_critSec);
  103. HRESULT hr = S_OK;
  104. long nCountOld = interfaceMappingListOld.GetCount();
  105. long nCountNew = interfaceMappingListNew.GetCount();
  106. for(long n = 0; n < nCountOld && SUCCEEDED(hr); ++n)
  107. {
  108. BOOL bDirty = TRUE;
  109. for(long m = 0; m < nCountNew; ++m)
  110. {
  111. if(interfaceMappingListOld[n].m_guidInterface == interfaceMappingListNew[m].m_guidInterface)
  112. {
  113. if(interfaceMappingListOld[n].m_dwIpAddress == interfaceMappingListNew[m].m_dwIpAddress)
  114. {
  115. bDirty = FALSE;
  116. }
  117. break;
  118. }
  119. }
  120. if(bDirty)
  121. {
  122. hr = interfaceList.HrPushBack(interfaceMappingListOld[n].m_guidInterface);
  123. }
  124. }
  125. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceHelper::HrGenerateDirtyGuidList");
  126. return hr;
  127. }