Leaked source code of windows server 2003
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.

219 lines
5.8 KiB

  1. /*
  2. File GuidMap.c
  3. Defines function to map a guid interface name to an unique descriptive
  4. name describing that interface and vice versa.
  5. Paul Mayfield, 8/25/97
  6. Copyright 1997, Microsoft Corporation.
  7. */
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. DWORD
  11. NsGetFriendlyNameFromIfName(
  12. IN HANDLE hMprConfig,
  13. IN LPCWSTR pwszName,
  14. OUT LPWSTR pwszBuffer,
  15. IN PDWORD pdwBufSize
  16. )
  17. /*++
  18. Arguments:
  19. hMprConfig - Handle to the MprConfig
  20. pwszName - Buffer holding the Guid Interface Name
  21. pwszBuffer - Buffer to hold the Friendly Interface Name
  22. pdwBufSize - pointer to, size (in Bytes) of the pwszBuffer buffer
  23. --*/
  24. {
  25. DWORD dwErr;
  26. if ((pdwBufSize == NULL) || (*pdwBufSize == 0) || (pwszName == NULL))
  27. {
  28. return ERROR_CAN_NOT_COMPLETE;
  29. }
  30. if (g_pwszRouterName is NULL)
  31. {
  32. GUID Guid;
  33. UNICODE_STRING us;
  34. NTSTATUS ntStatus;
  35. //
  36. // If we're operating on the local machine, just use IPHLPAPI
  37. // which works for some ras client interfaces too. The Mpr
  38. // API will fail for all ras client interfaces, but it's
  39. // remotable whereas IPHLPAPI is not.
  40. //
  41. RtlInitUnicodeString(&us, pwszName);
  42. ntStatus = RtlGUIDFromString(&us, &Guid);
  43. if (ntStatus == STATUS_SUCCESS)
  44. {
  45. dwErr = NhGetInterfaceNameFromGuid(
  46. &Guid,
  47. pwszBuffer,
  48. pdwBufSize,
  49. FALSE,
  50. FALSE);
  51. if (dwErr == NO_ERROR)
  52. {
  53. return dwErr;
  54. }
  55. }
  56. }
  57. if (hMprConfig == NULL)
  58. {
  59. return ERROR_CAN_NOT_COMPLETE;
  60. }
  61. dwErr = MprConfigGetFriendlyName(hMprConfig,
  62. (LPWSTR)pwszName,
  63. pwszBuffer,
  64. *pdwBufSize);
  65. if(dwErr isnot NO_ERROR)
  66. {
  67. HANDLE hIfHandle;
  68. dwErr = MprConfigInterfaceGetHandle(hMprConfig,
  69. (LPWSTR)pwszName,
  70. &hIfHandle);
  71. if (dwErr is NO_ERROR)
  72. {
  73. wcsncpy(pwszBuffer,
  74. pwszName,
  75. (*pdwBufSize)/sizeof(WCHAR));
  76. }
  77. else
  78. {
  79. dwErr = ERROR_NO_SUCH_INTERFACE ;
  80. }
  81. }
  82. return dwErr;
  83. }
  84. DWORD
  85. NsGetIfNameFromFriendlyName(
  86. IN HANDLE hMprConfig,
  87. IN LPCWSTR pwszName,
  88. OUT LPWSTR pwszBuffer,
  89. IN PDWORD pdwBufSize
  90. )
  91. /*++
  92. Arguments:
  93. hMprConfig - Handle to the MprConfig
  94. pwszName - Buffer holding the Friendly Interface Name
  95. pwszBuffer - Buffer to hold the Guid Interface Name
  96. pdwBufSize - pointer to, size (in Bytes) of the pwszBuffer buffer
  97. Return:
  98. NO_ERROR, ERROR_NO_SUCH_INTERFACE
  99. --*/
  100. {
  101. DWORD dwErr, i, dwCount, dwTotal, dwSize;
  102. HANDLE hIfHandle;
  103. PMPR_INTERFACE_0 pmi0;
  104. WCHAR wszFriendlyName[MAX_INTERFACE_NAME_LEN+1];
  105. if((hMprConfig == NULL) ||
  106. (pdwBufSize == NULL) ||
  107. (*pdwBufSize == 0))
  108. {
  109. return ERROR_CAN_NOT_COMPLETE;
  110. }
  111. // First try to map a friendly name to a GUID name
  112. dwErr = MprConfigGetGuidName(hMprConfig,
  113. (LPWSTR)pwszName,
  114. pwszBuffer,
  115. *pdwBufSize);
  116. if (dwErr isnot ERROR_NOT_FOUND)
  117. {
  118. return dwErr;
  119. }
  120. // Next see if the friendly name is the same as an interface name
  121. dwErr = MprConfigInterfaceGetHandle(hMprConfig,
  122. (LPWSTR)pwszName,
  123. &hIfHandle);
  124. if (dwErr is NO_ERROR)
  125. {
  126. wcsncpy(pwszBuffer,
  127. pwszName,
  128. (*pdwBufSize)/sizeof(WCHAR));
  129. }
  130. if (dwErr isnot ERROR_NO_SUCH_INTERFACE)
  131. {
  132. return dwErr;
  133. }
  134. // Exact match failed, try a longest match by enumerating
  135. // all interfaces and comparing friendly names (yes this
  136. // can be slow, but I can't think of any other way offhand
  137. // to allow interface names to be abbreviated)
  138. dwErr = MprConfigInterfaceEnum( hMprConfig,
  139. 0,
  140. (LPBYTE*) &pmi0,
  141. (DWORD) -1,
  142. &dwCount,
  143. &dwTotal,
  144. NULL );
  145. if (dwErr isnot NO_ERROR)
  146. {
  147. return dwErr;
  148. }
  149. dwErr = ERROR_NO_SUCH_INTERFACE;
  150. for (i=0; i<dwCount; i++)
  151. {
  152. DWORD dwRet;
  153. // Get interface friendly name
  154. dwSize = sizeof(wszFriendlyName);
  155. dwRet = NsGetFriendlyNameFromIfName( hMprConfig,
  156. pmi0[i].wszInterfaceName,
  157. wszFriendlyName,
  158. &dwSize );
  159. if(dwRet is NO_ERROR)
  160. {
  161. //
  162. // Check for substring match
  163. //
  164. if (MatchToken( pwszName, wszFriendlyName))
  165. {
  166. wcsncpy(pwszBuffer,
  167. pmi0[i].wszInterfaceName,
  168. (*pdwBufSize)/sizeof(WCHAR));
  169. dwErr = NO_ERROR;
  170. break;
  171. }
  172. }
  173. }
  174. MprConfigBufferFree(pmi0);
  175. return dwErr;
  176. }