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.

174 lines
4.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: I N T E R F A C E M A N A G E R . C P P
  7. //
  8. // Contents: Manages building the list of IP addresses
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 3 Jan 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "InterfaceManager.h"
  18. #include "winsock2.h"
  19. CInterfaceManager::CInterfaceManager()
  20. {
  21. }
  22. CInterfaceManager::~CInterfaceManager()
  23. {
  24. }
  25. HRESULT CInterfaceManager::HrInitializeWithAllInterfaces()
  26. {
  27. HRESULT hr = S_OK;
  28. // Preallocate mapping table
  29. m_bAllInterfaces = TRUE;
  30. m_interfaceMappingList.Clear();
  31. hr = HrProcessIpAddresses();
  32. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrInitializeWithAllInterfaces");
  33. return hr;
  34. }
  35. HRESULT CInterfaceManager::HrInitializeWithIncludedInterfaces(const InterfaceList & interfaceList)
  36. {
  37. HRESULT hr = S_OK;
  38. m_bAllInterfaces = FALSE;
  39. long nCount = interfaceList.GetCount();
  40. // Preallocate mapping table
  41. m_interfaceMappingList.Clear();
  42. hr = m_interfaceMappingList.HrSetCount(nCount);
  43. if(SUCCEEDED(hr))
  44. {
  45. for(long n = 0; n < nCount; ++n)
  46. {
  47. m_interfaceMappingList[n].m_guidInterface = interfaceList[n];
  48. m_interfaceMappingList[n].m_dwIpAddress = 0;
  49. }
  50. hr = HrProcessIpAddresses();
  51. }
  52. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrInitializeWithIncludedInterfaces");
  53. return hr;
  54. }
  55. HRESULT CInterfaceManager::HrGetValidIpAddresses(IpAddressList & ipAddressList)
  56. {
  57. HRESULT hr = S_OK;
  58. long nCount = m_interfaceMappingList.GetCount();
  59. ipAddressList.Clear();
  60. hr = ipAddressList.HrSetCount(nCount + 1);
  61. if(SUCCEEDED(hr))
  62. {
  63. for(long n = 0; n < nCount; ++n)
  64. {
  65. ipAddressList[n] = m_interfaceMappingList[n].m_dwIpAddress;
  66. }
  67. // Add loopback address
  68. ipAddressList[n] = inet_addr("127.0.0.1");
  69. }
  70. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrGetValidIpAddresses");
  71. return hr;
  72. }
  73. HRESULT CInterfaceManager::HrGetValidIndices(IndexList & indexList)
  74. {
  75. HRESULT hr = S_OK;
  76. long nCount = m_interfaceMappingList.GetCount();
  77. indexList.Clear();
  78. hr = indexList.HrSetCount(nCount);
  79. if(SUCCEEDED(hr))
  80. {
  81. for(long n = 0; n < nCount; ++n)
  82. {
  83. indexList[n] = m_interfaceMappingList[n].m_dwIndex;
  84. }
  85. }
  86. // Loopback case is handled separately but in HrGetValidIpAddresses() we have Loopback entry.
  87. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrGetValidIndices");
  88. return hr;
  89. }
  90. HRESULT CInterfaceManager::HrAddInterfaceMappingIfPresent(DWORD dwIpAddress, DWORD dwIndex, const GUID & guidInterface)
  91. {
  92. HRESULT hr = S_OK;
  93. if(m_bAllInterfaces)
  94. {
  95. //InterfaceMapping interfaceMapping = {guidInterface, dwIpAddress};
  96. InterfaceMapping interfaceMapping;
  97. interfaceMapping.m_dwIpAddress = dwIpAddress;
  98. interfaceMapping.m_dwIndex = dwIndex;
  99. interfaceMapping.m_guidInterface = guidInterface;
  100. hr = m_interfaceMappingList.HrPushBack(interfaceMapping);
  101. }
  102. else
  103. {
  104. long nCount = m_interfaceMappingList.GetCount();
  105. for(long n = 0; n < nCount; ++n)
  106. {
  107. if(m_interfaceMappingList[n].m_guidInterface == guidInterface)
  108. {
  109. m_interfaceMappingList[n].m_dwIndex = dwIndex;
  110. m_interfaceMappingList[n].m_dwIpAddress = dwIpAddress;
  111. break;
  112. }
  113. }
  114. }
  115. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrAddInterfaceMappingIfPresent");
  116. return hr;
  117. }
  118. HRESULT CInterfaceManager::HrProcessIpAddresses()
  119. {
  120. HRESULT hr = S_OK;
  121. CInterfaceTable interfaceTable;
  122. hr = interfaceTable.HrInitialize();
  123. if(SUCCEEDED(hr))
  124. {
  125. InterfaceMappingList interfaceMappingList;
  126. hr = interfaceTable.HrGetMappingList(interfaceMappingList);
  127. if(SUCCEEDED(hr))
  128. {
  129. long nCount = interfaceMappingList.GetCount();
  130. for(long n = 0; n < nCount && SUCCEEDED(hr); ++n)
  131. {
  132. hr = HrAddInterfaceMappingIfPresent(
  133. interfaceMappingList[n].m_dwIpAddress,
  134. interfaceMappingList[n].m_dwIndex,
  135. interfaceMappingList[n].m_guidInterface);
  136. }
  137. }
  138. }
  139. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrProcessIpAddresses");
  140. return hr;
  141. }
  142. HRESULT CInterfaceManager::HrGetMappingList(InterfaceMappingList & interfaceMappingList)
  143. {
  144. HRESULT hr = S_OK;
  145. interfaceMappingList.Swap(m_interfaceMappingList);
  146. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceManager::HrGetMappingList");
  147. return hr;
  148. }