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.

333 lines
7.6 KiB

  1. //=============================================================================
  2. // Copyright (c) Microsoft Corporation
  3. // Abstract:
  4. // This module implements ifindex-name conversion functions.
  5. //=============================================================================
  6. #include "precomp.h"
  7. #pragma hdrstop
  8. #define MAX_FRIENDLY_NAME_LENGTH 2000
  9. HANDLE g_hMprConfig = INVALID_HANDLE_VALUE;
  10. DWORD
  11. Connect()
  12. {
  13. return MprConfigServerConnect(NULL, &g_hMprConfig);
  14. }
  15. VOID
  16. Disconnect()
  17. {
  18. MprConfigServerDisconnect(g_hMprConfig);
  19. g_hMprConfig = INVALID_HANDLE_VALUE;
  20. }
  21. DWORD
  22. MapAdapterNameToFriendlyName(
  23. IN PWCHAR pwszMachine,
  24. IN LPSTR AdapterName,
  25. IN PIP_ADAPTER_ADDRESSES pAdapterInfo,
  26. OUT PWCHAR *ppwszFriendlyName
  27. )
  28. /*++
  29. Routine Description:
  30. Maps an adapter GUID to an interface friendly name. This is IPv4/IPv6
  31. agnostic.
  32. Arguments:
  33. AdapterName - Supplies an adapter GUID.
  34. ppwszFriendlyName - Receives a pointer to a static buffer containing
  35. the interface friendly name.
  36. --*/
  37. {
  38. PIP_ADAPTER_ADDRESSES pIf;
  39. for (pIf = pAdapterInfo; pIf; pIf = pIf->Next) {
  40. if (!strcmp(AdapterName, pIf->AdapterName)) {
  41. *ppwszFriendlyName = pIf->FriendlyName;
  42. return NO_ERROR;
  43. }
  44. }
  45. return ERROR_NOT_FOUND;
  46. }
  47. #define GUID_FORMAT_A "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
  48. VOID
  49. ConvertGuidToStringA(
  50. IN GUID *pGuid,
  51. OUT PCHAR pszBuffer
  52. )
  53. {
  54. sprintf(pszBuffer, GUID_FORMAT_A,
  55. pGuid->Data1,
  56. pGuid->Data2,
  57. pGuid->Data3,
  58. pGuid->Data4[0],
  59. pGuid->Data4[1],
  60. pGuid->Data4[2],
  61. pGuid->Data4[3],
  62. pGuid->Data4[4],
  63. pGuid->Data4[5],
  64. pGuid->Data4[6],
  65. pGuid->Data4[7]);
  66. }
  67. DWORD
  68. MapGuidToFriendlyName(
  69. IN PWCHAR pwszMachine,
  70. IN GUID *pGuid,
  71. IN PIP_ADAPTER_ADDRESSES pAdapterInfo,
  72. OUT PWCHAR *ppwszFriendlyName
  73. )
  74. {
  75. CHAR szBuffer[80];
  76. ConvertGuidToStringA(pGuid, szBuffer);
  77. return MapAdapterNameToFriendlyName(pwszMachine, szBuffer,
  78. pAdapterInfo, ppwszFriendlyName);
  79. }
  80. DWORD
  81. MapFriendlyNameToAdapterName(
  82. IN PWCHAR pwszMachine,
  83. IN PWCHAR pwszFriendlyName,
  84. IN PIP_ADAPTER_ADDRESSES pAdapterInfo,
  85. OUT LPSTR *AdapterName
  86. )
  87. /*++
  88. Routine Description:
  89. Maps an interface friendly name to an adapter GUID. This is IPv4/IPv6
  90. agnostic.
  91. Arguments:
  92. pwszFriendlyName - Supplies an interface friendly name.
  93. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  94. AdapterName - Receives a pointer to a static buffer containing
  95. the adapter GUID.
  96. --*/
  97. {
  98. PIP_ADAPTER_ADDRESSES pIf;
  99. //
  100. // First look for an exact match.
  101. //
  102. for (pIf = pAdapterInfo; pIf; pIf = pIf->Next) {
  103. if (!_wcsicmp(pwszFriendlyName, pIf->FriendlyName)) {
  104. *AdapterName = pIf->AdapterName;
  105. return NO_ERROR;
  106. }
  107. }
  108. //
  109. // Then look for a partial match.
  110. //
  111. for (pIf = pAdapterInfo; pIf; pIf = pIf->Next) {
  112. if (!_wcsnicmp(pwszFriendlyName, pIf->FriendlyName,
  113. wcslen(pwszFriendlyName))) {
  114. *AdapterName = pIf->AdapterName;
  115. return NO_ERROR;
  116. }
  117. }
  118. return ERROR_NOT_FOUND;
  119. }
  120. DWORD
  121. MapAdapterNameToIfIndex(
  122. IN LPSTR AdapterName,
  123. IN PIP_ADAPTER_ADDRESSES pAdapterInfo,
  124. IN DWORD dwFamily,
  125. OUT DWORD *pdwIfIndex
  126. )
  127. /*++
  128. Routine Description:
  129. Maps an adapter GUID to an interface index. This is IPv4/IPv6
  130. specific, since each has a separate ifindex.
  131. Arguments:
  132. AdapterName - Supplies an adapter GUID.
  133. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  134. dwFamily - Supplies the protocol for which an ifindex is needed.
  135. pdwIfIndex - Receives the ifindex value.
  136. --*/
  137. {
  138. PIP_ADAPTER_ADDRESSES pIf;
  139. for (pIf=pAdapterInfo; pIf; pIf=pIf->Next) {
  140. if (!strcmp(pIf->AdapterName, AdapterName)) {
  141. break;
  142. }
  143. }
  144. if (!pIf) {
  145. *pdwIfIndex = 0;
  146. return ERROR_NOT_FOUND;
  147. }
  148. *pdwIfIndex = (dwFamily == AF_INET6)? pIf->Ipv6IfIndex : pIf->IfIndex;
  149. return NO_ERROR;
  150. }
  151. PIP_ADAPTER_ADDRESSES
  152. MapIfIndexToAdapter(
  153. IN DWORD dwFamily,
  154. IN DWORD dwIfIndex,
  155. IN PIP_ADAPTER_ADDRESSES pAdapterInfo
  156. )
  157. /*++
  158. Routine Description:
  159. Maps an interface index to an adapter entry. This is IPv4/IPv6
  160. specific, since each has a separate ifindex.
  161. Arguments:
  162. dwFamily - Supplies the protocol.
  163. dwIfIndex - Supplies the interface index to map.
  164. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  165. Returns:
  166. Adapter entry if found, NULL if not.
  167. --*/
  168. {
  169. PIP_ADAPTER_ADDRESSES pIf;
  170. for (pIf=pAdapterInfo; pIf; pIf=pIf->Next) {
  171. if ((dwFamily == AF_INET) && (pIf->IfIndex == dwIfIndex)) {
  172. break;
  173. }
  174. if ((dwFamily == AF_INET6) && (pIf->Ipv6IfIndex == dwIfIndex)) {
  175. break;
  176. }
  177. }
  178. if (!pIf) {
  179. return NULL;
  180. }
  181. return pIf;
  182. }
  183. LPSTR
  184. MapIfIndexToAdapterName(
  185. IN DWORD dwFamily,
  186. IN DWORD dwIfIndex,
  187. IN IP_ADAPTER_ADDRESSES *pAdapterInfo
  188. )
  189. /*++
  190. Routine Description:
  191. Maps an interface index to an adapter GUID. This is IPv4/IPv6
  192. specific, since each has a separate ifindex.
  193. Arguments:
  194. dwFamily - Supplies the protocol.
  195. dwIfIndex - Supplies the interface index to map.
  196. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  197. Returns:
  198. Adapter name if found, NULL if not.
  199. --*/
  200. {
  201. PIP_ADAPTER_ADDRESSES pIf;
  202. pIf = MapIfIndexToAdapter(dwFamily, dwIfIndex, pAdapterInfo);
  203. return (pIf)? pIf->AdapterName : NULL;
  204. }
  205. DWORD
  206. MapFriendlyNameToIpv6IfIndex(
  207. IN PWCHAR pwszFriendlyName,
  208. IN PIP_ADAPTER_ADDRESSES pAdapterInfo,
  209. OUT DWORD *pdwIfIndex
  210. )
  211. /*++
  212. Routine Description:
  213. Maps an interface friendly name to an interface index. This is IPv6
  214. specific, since IPv4 and IPv6 have separate ifindexes.
  215. Arguments:
  216. pwszFriendlyName - Supplies the friendly name to map.
  217. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  218. pdwIfIndex - Receives the ifindex value.
  219. --*/
  220. {
  221. DWORD dwErr, i;
  222. LPSTR AdapterName;
  223. PWCHAR pwszTemp;
  224. //
  225. // If string only contains digits, treat it as an IfIndex
  226. //
  227. if (wcsspn(pwszFriendlyName, L"1234567890") == wcslen(pwszFriendlyName)) {
  228. *pdwIfIndex = wcstoul(pwszFriendlyName, NULL, 10);
  229. return NO_ERROR;
  230. }
  231. dwErr = MapFriendlyNameToAdapterName(NULL, pwszFriendlyName, pAdapterInfo,
  232. &AdapterName);
  233. if (dwErr != NO_ERROR) {
  234. return dwErr;
  235. }
  236. return MapAdapterNameToIfIndex(AdapterName, pAdapterInfo, AF_INET6, pdwIfIndex);
  237. }
  238. DWORD
  239. MapIpv6IfIndexToFriendlyName(
  240. IN DWORD dwIfIndex,
  241. IN IP_ADAPTER_ADDRESSES *pAdapterInfo,
  242. OUT PWCHAR *ppwszFriendlyName
  243. )
  244. /*++
  245. Routine Description:
  246. Maps an interface index to a friendly name. This is IPv6
  247. specific, since IPv4 and IPv6 have separate ifindexes.
  248. Arguments:
  249. dwIfIndex - Supplies the ifindex value.
  250. pAdapterInfo - Supplies info obtained from GetAdaptersAddresses().
  251. ppwszFriendlyName - Receives a pointer to a static buffer containing
  252. the interface friendly name.
  253. --*/
  254. {
  255. IP_ADAPTER_ADDRESSES *If;
  256. for (If=pAdapterInfo; If; If=If->Next) {
  257. if (If->Ipv6IfIndex == dwIfIndex) {
  258. *ppwszFriendlyName = If->FriendlyName;
  259. return NO_ERROR;
  260. }
  261. }
  262. return ERROR_NOT_FOUND;
  263. }