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.

132 lines
3.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: I N T E R F A C E T A B L E . C P P
  7. //
  8. // Contents: Builds a mapping from IP addresses to interface guids
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 7 Feb 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "InterfaceTable.h"
  18. #include <winsock2.h>
  19. #include <iphlpapi.h>
  20. CInterfaceTable::CInterfaceTable()
  21. {
  22. }
  23. CInterfaceTable::~CInterfaceTable()
  24. {
  25. }
  26. HRESULT CInterfaceTable::HrInitialize()
  27. {
  28. HRESULT hr = S_OK;
  29. PIP_ADAPTER_INFO pip = NULL;
  30. ULONG ulSize = 0;
  31. GetAdaptersInfo(NULL, &ulSize);
  32. if(ulSize)
  33. {
  34. pip = reinterpret_cast<PIP_ADAPTER_INFO>(malloc(ulSize));
  35. DWORD dwRet = GetAdaptersInfo(pip, &ulSize);
  36. hr = HRESULT_FROM_WIN32(dwRet);
  37. if(SUCCEEDED(hr))
  38. {
  39. PIP_ADAPTER_INFO pipIter = pip;
  40. while(pipIter && SUCCEEDED(hr))
  41. {
  42. wchar_t szAdapter[MAX_ADAPTER_NAME_LENGTH + 4];
  43. wchar_t * pchAdapter = szAdapter;
  44. wchar_t * pchAdapterEnd = szAdapter;
  45. MultiByteToWideChar(CP_ACP, 0, pipIter->AdapterName, -1, szAdapter, MAX_ADAPTER_NAME_LENGTH + 4);
  46. // Make sure it's not an empty string first
  47. if (*pchAdapter)
  48. {
  49. // skip over the '{'
  50. pchAdapter++;
  51. // chop off the '}'
  52. pchAdapterEnd = wcschr(szAdapter, '\0');
  53. if (pchAdapterEnd)
  54. {
  55. pchAdapterEnd--;
  56. *pchAdapterEnd = 0;
  57. }
  58. }
  59. GUID guidInterface;
  60. if (RPC_S_OK == UuidFromString(pchAdapter, &guidInterface))
  61. {
  62. hr = S_OK;
  63. }
  64. else
  65. {
  66. hr = E_FAIL;
  67. }
  68. PIP_ADDR_STRING pipaddr = &pipIter->IpAddressList;
  69. while(pipaddr && SUCCEEDED(hr))
  70. {
  71. InterfaceMapping interfaceMapping;
  72. interfaceMapping.m_guidInterface = guidInterface;
  73. interfaceMapping.m_dwIpAddress = inet_addr(pipaddr->IpAddress.String);
  74. interfaceMapping.m_dwIndex = htonl(pipIter->Index);
  75. if(interfaceMapping.m_dwIpAddress)
  76. {
  77. hr = m_interfaceMappingList.HrPushBack(interfaceMapping);
  78. }
  79. pipaddr = pipaddr->Next;
  80. }
  81. pipIter = pipIter->Next;
  82. }
  83. }
  84. free(pip);
  85. }
  86. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceTable::HrInitialize");
  87. return hr;
  88. }
  89. HRESULT CInterfaceTable::HrMapIpAddressToGuid(DWORD dwIpAddress, GUID & guidInterface)
  90. {
  91. HRESULT hr = S_OK;
  92. ZeroMemory(&guidInterface, sizeof(GUID));
  93. long nCount = m_interfaceMappingList.GetCount();
  94. for(long n = 0; n < nCount; ++n)
  95. {
  96. if(m_interfaceMappingList[n].m_dwIpAddress == dwIpAddress)
  97. {
  98. guidInterface = m_interfaceMappingList[n].m_guidInterface;
  99. break;
  100. }
  101. }
  102. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceTable::HrMapIpAddressToGuid");
  103. return hr;
  104. }
  105. HRESULT CInterfaceTable::HrGetMappingList(InterfaceMappingList & interfaceMappingList)
  106. {
  107. HRESULT hr = S_OK;
  108. interfaceMappingList.Swap(m_interfaceMappingList);
  109. TraceHr(ttidSsdpNetwork, FAL, hr, FALSE, "CInterfaceTable::HrGetMappingList");
  110. return hr;
  111. }